Meaningless Drivel is fun!
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes a doubt.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "a doubt...." Watch "a doubt...." New topic

a doubt....

priya selva

Joined: Feb 21, 2002
Posts: 29
if( new Boolean("true") == new Boolean("true"))
y ans is false?...
Garann Means
Ranch Hand

Joined: Jan 28, 2002
Posts: 214
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))
if ((new Boolean("true")).equals((new Boolean("true"))))
Does that sound right?
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
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.

SCJP2. Please Indent your code using UBB Code
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
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()).
I agree. Here's the link:
subject: a doubt....
It's not a secret anymore!