File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes What this code does Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What this code does" Watch "What this code does" New topic
Author

What this code does

Noorulain Khan
Greenhorn

Joined: Jul 31, 2001
Posts: 28
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
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
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://aspose.com/file-tools
 
subject: What this code does