Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

booleans

 
Marc Bautis
Greenhorn
Posts: 6
  • 0
  • 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
  • 0
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic