Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

a doubt....

 
priya selva
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if( new Boolean("true") == new Boolean("true"))
System.out.println("True");
else
System.out.println("False");
y ans is false?...
 
Garann Means
Ranch Hand
Posts: 214
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Priya,
I think it's because you're asking whether one new Boolean object is the same as another new Boolean object. The answer is no because they are two different objects (though they contain the same value).
To get an answer of "True", you could ask either:
if ((boolean x = true) == (boolean y = true))
or..
if ((new Boolean("true")).equals((new Boolean("true"))))
Does that sound right?
g.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please the general subjects as "a doubt" or "help" are a bad choice because they don't give a clue about the real matter. People cannot decide if they are interested in the post beforehand. Also it is not very helpful for searches.
Thanks.
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to compare the addresses in memory of two object references, use ==
If you want to compare the state of two objects, use equals() (except when using equals() of Object class, which behaves like ==. Any other class which does not override this also inherits the same behavior for equals()).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic