• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Operators

 
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator



predict the output with explanation.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you try executing that piece of code? What was the results? What do you conclude from that?
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you predict it ?
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the output would be "b is true" ..... no wait ..... "b is false" ..... no ..... what?
 
sumedha rao
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the output was: b is true
but i never understood how the output got printed as true when b is false

Ok i understand it now...

when you use "=" it means you are actually SETTING it to true whereas when you use "==" you are COMPARING it to true

i dont know if anyone understands my explanation,but im trying my best to make it clear

boolean b=false;
if(b=true)
System.out.println("b is true");//it is being set to true(checked if b=false is true/correct),hence the output will be b is true


Now consider the following:

boolean b=false;
if(b==true) //compared
System.out.println("b is true");//here it is being compared,hence nothing will be printed(since b is false).

I hope this is clear
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

sumedha rao wrote: when you use "=" it means you are actually SETTING it to true whereas when you use "==" you are COMPARING it to true



So you have analysed it yourself

And its always better to use


Update: The discussions towards the end of this forum post are related to your query.
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or you can use if(true == b) so that if you type if(true = b) you'll get an error. This was very useful in C++ as in C++ int was used as boolean where 0 was false and everything else was true so if(i = 10) type mistakes were common...
 
reply
    Bookmark Topic Watch Topic
  • New Topic