• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

boolean value in "IF"

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)".
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 348
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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>
 
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
They worship nothing. They say it's because nothing is worth fighting for. Like this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic