• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Operators

 
Ranch Hand
Posts: 115
Firefox Browser Tomcat Server Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Can you predict it ?
 
Sheriff
Posts: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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: 9671
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • 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...
 
Or we might never have existed at all. Freaky. So we should cherish everything. Even this tiny ad:
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!