aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes booleans Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "booleans" Watch "booleans" New topic
Author

booleans

Marc Bautis
Greenhorn

Joined: Sep 17, 2004
Posts: 6
I have a question regarding why when the following is run the statement "b is true" is printed to the screen. I would think that I am setting b to be false before the if statement runs. The first check also sets it to false so I would think that b is false would be printed.

boolean b = false;
if(b = false) {
System.out.println("b is false");
} else if(b = true){
System.out.println("b is true");
} else {
System.out.println("b is not true or false");
}
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
You are typing "b=false" and "b=true" when you meant "b==false" and "b==true".

"if (b=true) ..." always assigns true to b and the if block always executes.


Mike Gershman
SCJP 1.4, SCWCD in process
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: booleans