• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals doubt??

 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Guys,

Why the code below prints false??

Object 01 = new Object();
Object 02 = new Object();

o1.equals(o2) //prints false??How and why??
 
anvesh charuvaka
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Object class's equals method compares the value of the reference variables i.e. what you get out of o1.equals(o2) is just what you get from
o1 == o2 and which is false because they are refering to different object.
 
Anuragk kushwaha
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i conclude as--
both the == operator and equals() methods, used with primitive datatype and with object references as well.
when == operator used with primitive datatype it works as equals()method. In similar way when equals() used with object references it works like == opeartor..

please correct me if above conclusion is wrong..
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by anuragk kushwaha:
i conclude as--
both the == operator and equals() methods, used with primitive datatype and with object references as well.
when == operator used with primitive datatype it works as equals()method. In similar way when equals() used with object references it works like == opeartor..

please correct me if above conclusion is wrong..


Why do you change the topic away from comparing object references to comparing primitives? It only goes to confuse the situation. Apart from that I do not understand what you are trying to say about "when == operator used with primitive datatype it works as equals()method." There is no equals method for primitive types. To compare primitive types, only the bit patterns in the variables are compared.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Back to the original problem. As anvesh wrote, comparing two Object (not subclasses of Object) instances with Object's equals is equivalent to comparing the two references themselves with ==. The references are different because they refer to different objects.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have one doubt in this :

I have tried this :



The above code will give output: Equals

and when i try to run :



it give output : Not Equals

Can you please tell me why this is happening as in above bith cases , we are comparing objects but different answer in each scenerio.
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In class Object, equals() method is defined as


i.e equals() method compares the reference values & not the contents.

But in String class, it is overridden to check the contents.
when new String() is called, it stores an empty string.
Both empty string contents match & Equals is printed in the first case.

Hope this helps.
 
ram gaurav
Ranch Hand
Posts: 208
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.
Your answer have cleared out my all doubts.

Thanks
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vinayagar,

Wondeful explanation. It got my doubts cleared as well. One more silly question to this will be...you told in your post that the String clas overrides the equals method...Can you place that paricular piece of code for the equals method in the String class??
 
Vinayagar Karpagam
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Here is the equals() method of String class.
 
Joe Harry
Ranch Hand
Posts: 10080
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Vinayagar.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic