• 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:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Boolean == Vs .equals()

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Cannot understand Why this happens:


Somebody please explain!!
Thanks,
Sindhur.
 
Ranch Hand
Posts: 451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sindhur,
Welcome to JavaRanch.
The reason is that in the case of b and b1, you are actually assigning them to the same object which is a public static final field in the Boolean class. In the case of b2 and b3, you are actually creating 2 new objects which have equal value but do not occupy the same memory space. See the explanation in the Javadoc.
[ February 26, 2004: Message edited by: Ken Krebs ]
 
Ranch Hand
Posts: 3271
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I also wrote up an explanation of this some time back and have it sitting on my website. You can view it here. Hopefully, that will clear up any questions you have. If not, let me know.
Corey
 
Ranch Hand
Posts: 94
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sindhur:
Your doubt stems from confusion regarding functioning of == and the .equals() method of the object.
Lets start with ==.
When we use ==, to compare 2 references, we are testing if both are pointing at the same object. In your case b2 and b3 are pointing to 2 different objects hence it returns false.
next in case of .equals()
this method compares the values contained in the object. In case of the wrapper classses (String, Integer, Boolean...) it has been implemented to compare the contents of the objects. so b2.equals(b3) will return true since both b2 and b3 both contain the value true.

Next, Boolean.valueOf() is a static method that returns the static variable Boolean.TRUE or Boolean.FALSE. Only 1 copy of these variables exists for the Boolean class. So all the Boolean objects created using the Boolean.valueOf() methods will point to either of these static objects.
hence b==b1 will be true and b.equals(b1) will be true.
 
Sindhur Sat
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the Good answers.
Sindhur.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic