File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes boolean value in 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 "boolean value in "IF"" Watch "boolean value in "IF"" New topic
Author

boolean value in "IF"

Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
When I say
boolean b = true;
if(b = true) {//}
else {b) {}
Shouldn't it say a compile error as in the if it should be (b == true)instad of the above, Why this works juts fine.
Pls advise!
Thanks
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Hi,
Welcome to Java Ranch! We don't have many rules here, but we do have a naming policy. Please head over and change your display name when you have a chance.
It works because the value of an assignment expression is the assigned value; thus the result of "b = true" is "true", a boolean, which is perfectly OK. There's an easy way to prevent yourself from ever making this typo: never explicitly compare anything to a boolean literal. You can always write "if (b)" instead of "if (b == true)" and "if (!b)" instead of "if (b != true)".


[Jess in Action][AskingGoodQuestions]
krtek veliky
Greenhorn

Joined: Aug 26, 2003
Posts: 1
i don't think so. in the code you're doing two things -
1) assigning boolean true value to variable b
2) checking condition if(true)
which is both ok
chi Lin
Ranch Hand

Joined: Aug 24, 2001
Posts: 348
when use b=true, you assign true to reference b regardless its previous value. Code segment inside if(b=true) { } will be executed as it evaluated to TRUE.
when use b==true, you compare if b equal to true, in your case, code segment inside if(b==true) {} will also be executed as the comparsion result is TRUE.


not so smart guy still curious to learn new stuff every now and then
Shiwani Aggarwal
Greenhorn

Joined: Aug 21, 2003
Posts: 22
Thanks all
One of your pointed out on my name, Preeti is perfectly valid and my true name. (I understood that Pretty was not good!!) but just to let you know 3 out of every ten in India have this name. Whats wrong? Do you still suggest I need to change>
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

Originally posted by Preeti:
Thanks all
One of your pointed out on my name, Preeti is perfectly valid and my true name. (I understood that Pretty was not good!!) but just to let you know 3 out of every ten in India have this name. Whats wrong? Do you still suggest I need to change>

Preeti-
Hopefully you saw my reply in this thread. We ask that you have both a first and last name listed in your display name. Thanks so much for helping us maintain a nice sense of community here on the Ranch. Our naming policy is just one of the ways we try to keep things comfortable and professional
Thanks!
 
Don't get me started about those stupid light bulbs.
 
subject: boolean value in "IF"