What this code does public class ShortCkt { public static void main(String args[]) { int i = 0; boolean t = true; boolean f = false, b; b = (t && ((i++) == 0)); //This code b = (f && ((i+=2) > 0)); //And this code System.out.println(i); } }
------------------
bami bal
Greenhorn
Joined: Aug 27, 2001
Posts: 1
posted
0
The first line evaluates t (true). Both operands must be true for b to become true, so the second operand will also be evaluated. The second operand adds 1 to i. i is not equal to 0, so the second operand = false. Therefore, b becomes false. The second line first evaluates f. Because f = false, b cannot become true. The operator && prevents the second operand to be evaluated, so (i+=2) won't be executed, and the value of i remains 1. [This message has been edited by bami bal (edited August 27, 2001).]
Cindy Glass
"The Hood"
Sheriff
Joined: Sep 29, 2000
Posts: 8521
posted
0
Actually the first line is the same as b= true && (0 == 0); //the incrementing happens AFTER the compare so b=true. Now i is incremented to 1 b = false && (skipped) > 0); b = false Try it this way:
Prints out true 1 false
"JavaRanch, where the deer and the Certified play" - David O'Meara
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.