如果声明与&&在测试第二个条件之前,第一个条件必须为真 [英] If statement with && where the first condition needs to be true before testing the second
问题描述
很抱歉,如果这是重复的,但我甚至想不出一种搜索方式。我不确定要问这个问题的术语,所以我只会解决我的问题。
Sorry if this is a duplicate, but I can't even think of a way to search of this. I'm not sure of the terminology to ask the question, so I'll just walk through my problem.
从我的测试来看,似乎if语句将在之前退出试图去阵列[10]。我总是这样吗?意思是,如果我有&&在我的if语句中,左侧是false,是否会在测试第二个之前退出?就此而言,它是否总是首先测试左边?
From my tests, it seems the if statement will quit before attempting to go to go to array[10]. I that always the case? Meaning, if I have an && in my if statement and the left side is false, will it always exit before testing the second? And for that matter, will it always test the left first?
public static void main(String[] args) {
boolean [] array = new boolean [10];
Arrays.fill(array, true);
int i = 10;
if(i < array.length && array[i]){
System.out.println("WhoHoo!");
}
}
推荐答案
来自关于运营商的 Java教程:
&&和||运算符对两个布尔表达式执行Conditional-AND和Conditional-OR
操作。这些运算符表现出
短路行为,这意味着只有在需要时才会计算第二个操作数
。
The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.
这篇关于如果声明与&&在测试第二个条件之前,第一个条件必须为真的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!