• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

boolean value in "IF"

 
Shiwani Aggarwal
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)".
 
krtek veliky
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 348
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shiwani Aggarwal
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4313
Android IntelliJ IDE Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic