File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
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
I agree. Here's the link:
subject: booleans
It's not a secret anymore!