This week's book giveaways are in the iOS and Features new in Java 8 forums.
We're giving away four copies each of Barcodes with iOS: Bringing together the digital and physical worlds and Core Java for the Impatient and have the authors on-line!
See this thread and this one for details.
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 Barcodes with iOS this week in the iOS forum
or Core Java for the Impatient in the Java 8 forum!

JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "booleans" Watch "booleans" New topic


Marc Bautis

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
rocket mass heater kickstarter
subject: booleans