*
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 Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "bool?" Watch "bool?" New topic
Author

bool?

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?
Sonir
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.
 
Don't get me started about those stupid light bulbs.
 
subject: bool?
 
Similar Threads
why not initilazed?
Boolean Output?
If, boolean
How do I avoid null pitfalls when comparing Strings?
Doubt about boolean