It's not a secret anymore!
The moose likes Beginning Java and the fly likes boolean comparision Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Reply Bookmark "boolean comparision" Watch "boolean comparision" New topic
Author

boolean comparision

Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
Boolean b1 = new Boolean(“yes”);
Boolean b2 = new Boolean("no");
System.out.println(b1.equals(b2));

i'm geting result : true...

tha value in b1 is yes and b2 is no...

how it is possible to get true....

could you state the reason please...
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14670
    
  11

The answer is in the API of the Boolean class. Check it.


[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14670
    
  11

"Ramakrishna", you've been warned several times about our naming policy. Don't ignore the warnings, and change your name as explained in the Naming Policy.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Hi Ram,

If you construct a Boolean object other then with the "true" string, it evaluate it as false


Both evaluates false, and due to overriding equals() method it checks meaningfully equals which returns true.

welcome,

Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
thanq

Sudhakar Sharma wrote:Hi Ram,

If you construct a Boolean object other then with the "true" string, it evaluate it as false


Both evaluates false, and due to overriding equals() method it checks meaningfully equals which returns true.

welcome,

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 32638
    
    4
Read this, Sudhakar Sharma, which appears on the Beginning Java title page.
We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers.
By giving a straight answer, you have deprived Ramakrishna Konanki of the chance to learn for himself. Christophe's suggestion was far better; he would have remembered the answer longer, and learned about the API documentation, if he had looked it up for himself. It would only have taken a few seconds to find the answer from the API.
Sudhakar Sharma
Ranch Hand

Joined: Apr 04, 2009
Posts: 71

Thanks for reminding me the policy of the forum, next time I will care of this.
 
I agree. Here's the link: http://jrebel.com/download
 
subject: boolean comparision
 
Similar Threads
Something about auto-boxing/unboxing in SE5
Boxing query
Boolean doubt
Wrappers question
Boolean class