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 Operator question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Operator question" Watch "Operator question" New topic
Author

Operator question

cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
Hi all
Why is the following code printing 1
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t && ((i++) == 0));
b = (f && ((i+=2) > 0));
System.out.println(i);
this one 2
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);
and this one 3
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);
Can someone please explain it to me.I found it in a mock exam.
Thanks
Cornel
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
b = (t && ((i++) == 0));
b = (f && ((i+=2) > 0));
In the second statement, since there is no chance of the statement being true, the second half of the statement is never executed. That is what the && does - it short circuits the "and" as soon as it is impossible for it to be true.
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
The first statement will definitely be true as soon as the first half is analyzed so there is no reason for it to look at the second half. The second statement may or may not be true after analyzing the first half so it has to continue and analyze the second half.
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
The single & does not short circuit and always causes both halfs to be analyzed.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Originally posted by cornel jew:
Hi all
Why is the following code printing 1
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t && ((i++) == 0));
b = (f && ((i+=2) > 0));
System.out.println(i);

The key here is the behavior of &&. The second expression (the one to the right of the &&) is evaluated ONLY if the first expression (the one to the left of the &&) evaluates to true. So, in the first line, "i++" is evaluated because "t" is true, therefore the value of "i" is incremented to 1. In the next line, "f" is false, so the expression "i+=2" is skipped.
Originally posted by cornel jew:

this one 2
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t || ((i++) == 0));
b = (f || ((i+=2) > 0));
System.out.println(i);

The key here is the behavior of ||. The second expression (the one to the right of the ||) is evaluated ONLY if the first expression (the one to the left of the ||) evaluates to false. So, in the first line, "i++" is NOT evaluated because "t" is true, therefore the value of "i" is NOT incremented to 1. In the next line, "f" is false, so the "i+=2" gets evaluated, therfore the value of "i" becomes 2.
Originally posted by cornel jew:

and this one 3
int i = 0;
boolean t = true;
boolean f = false, b;
b = (t & ((i++) == 0));
b = (f & ((i+=2) > 0));
System.out.println(i);

When using the "&" operator, BOTH sides get evaluated no matter what, that's why the result is 3.
Hope that helps.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
cornel jew
Ranch Hand

Joined: Jul 13, 2002
Posts: 37
Hi again
Thanks for the wonderful forum and quick replies.
After reading your post i understand the questions,test myself on the other similar ones and scores full marks.
Yours
Corn
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Operator question