File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes bool? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "bool?" Watch "bool?" New topic


sonir shah
Ranch Hand

Joined: Nov 01, 2001
Posts: 435
Consider this:
boolean bool = true; int k = 10;
if( bool == false && ( (k = 3) == 5 ) ) { .....}
System.out.println(k); // this will print 10.
Ans : 10
But bool declared is true and then in the condition it checks that whether bool is false, the answer is false
Then why is the value of k printed?
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
if( bool == false && ( (k = 3) == 5 ) ) { .....}
System.out.println(k); // this will print 10.

because the if () {} block ends were marked bold.
It will not print out 3 because && uses short circuit optimization. Both conditions have to be true to execute the {}. If first condition it false the second condition will be allways skipped if you use &&.
Hope it helps.
wood burning stoves
subject: bool?
Similar Threads
why not initilazed?
If, boolean
Doubt about boolean
Boolean Output?
How do I avoid null pitfalls when comparing Strings?