This week's book giveaways are in the Refactoring and Agile forums.
We're giving away four copies each of Re-engineering Legacy Software and Docker in Action and have the authors on-line!
See this thread and this one for details.
Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

booleans

 
Marc Bautis
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1272
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic