3

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.

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!");
        }
}
Vitaly
  • 2,760
  • 2
  • 19
  • 26
Josh Horowitz
  • 655
  • 2
  • 6
  • 15
  • 1
    && is called short circuit operator. This is the reason behind it. – codeMan Apr 16 '13 at 09:18
  • take a look a the answer for this question : http://stackoverflow.com/questions/8759868/java-logcial-operators-short-circuiting – codeMan Apr 16 '13 at 09:19

4 Answers4

9

From the Java Tutorials on operators:

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.

yiannis
  • 1,421
  • 12
  • 21
2

Yes. If the left side is false, it will terminate the condition. Because if you put && then all the conditions must be true, if anyone condition turns false, then there is no need to go further. So it terminates when the condition gets false

iCode
  • 8,892
  • 21
  • 57
  • 91
1

if I have an && in my if statement and the left side is false, will it always exit before testing the second?

Yes. If the left side is false, there is no need to check the right side, because when you have:

if(a && b) { ... }

If !a, then the whole statement is evaluated to false no matter what b is.

If a, then it will continue to evaluate the next.

For summary, always remember this:

...the second operand is evaluated only if needed.

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • I just tested that, that doesn't seem to be the case. Are you sure? is my compiler just figuring out what I mean and doing some behind the scenes magic? – Josh Horowitz Apr 16 '13 at 09:22
  • I would like to add this : if you still want to evaluate `b` (e.g. because it is a method that returns a boolean) you need to type `(a & b)`. Using a single ampersand will not short circuit your evaluation. – jeroen_de_schutter Apr 16 '13 at 09:23
  • @Maroun I tried if((array.length < 10 && true) && array[i]) The program just terminated without throwing an array out of bounds exception – Josh Horowitz Apr 16 '13 at 09:24
  • Made a dumb mistake. Corrected code: if((i < array.length && true) && array[i]) – Josh Horowitz Apr 16 '13 at 09:26
  • 1
    @Maroun c will not be evaluated in the case you describe. – yiannis Apr 16 '13 at 09:28
  • @JamesFargotson `(i < array.length && true)` is evaluated to `false`, then there is no need to continue to `array[i]`. That's why it works. – Maroun Apr 16 '13 at 09:28
  • @yiannis Thanks. Edited.. Definitely time to sleep. – Maroun Apr 16 '13 at 09:28
0

It will only evaluate second condition if first condition evaluates to true.

The evaluation starts from left and when it evaluates to false further evaluation does not happen.

Bhushan Bhangale
  • 10,921
  • 5
  • 43
  • 71