This week's book giveaway is in the OCAJP forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line!
See this thread for details.
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 OCA Java SE 8 Programmer I Study Guide 1Z0-808 this week in the OCAJP 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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: bool?