The bottom line is that XOR
is higher precedence. So false
will be evaluated first and since it followed by and AND
the rest of the statement is not evaluated (&& short circuited the evaluation). But if you want to see it in action, here goes. In the example, I assign to local variables, otherwise the compiler in its infinite wisdom, will pre-evaluate the constants (like your example) and obtain the result. So it was never really executed by the JVM.
Given the following code:
boolean f = false;
boolean t1 = true;
boolean t2 = true;
boolean result = f && t1 ^ t2;
Here is the byteCode with explanations
public static void main(java.lang.String[] args);
0 iconst_0 // int 0 represents false
1 istore_1 [f] // store at f slot
2 iconst_1 // int 1 represents true
3 istore_2 [t1] // store at t1 slot
4 iconst_1
5 istore_3 [t2] // same for t2
6 iload_1 [f] // load f onto stack
7 ifeq 20 // if equal to 0 (false), go to 20 and store 0 in boolean
// result. This is the first indication that
// false was evaluated in isolation since false && any
// thing else would return false. So the XOR need not be
// evaluated. In your case it was `dead code`. But if `f`
// were actually set to true..
10 iload_2 [t1] // load t1 and t2 on stack
11 iload_3 [t2] //
12 ixor // now do the XOR
13 ifeq 20 // if result is true, statement is true
// Remember! We're only here because f was true.
16 iconst_1 // and load 1 (true onto stack) and store in result
17 goto 21 // else fall thru and store 0 in result.
20 iconst_0
21 istore 4 [result]
23 return