wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes equals 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 "equals doubt??" Watch "equals doubt??" New topic
Author

equals doubt??

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Guys,

Why the code below prints false??

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

o1.equals(o2) //prints false??How and why??


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
anvesh charuvaka
Greenhorn

Joined: Sep 25, 2006
Posts: 11
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

Joined: Aug 02, 2006
Posts: 51
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

Joined: Aug 03, 2002
Posts: 7729
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.


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
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

Joined: Mar 29, 2006
Posts: 208
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

Joined: Apr 09, 2006
Posts: 72
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

Joined: Mar 29, 2006
Posts: 208
Thanks a lot.
Your answer have cleared out my all doubts.

Thanks
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

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

Joined: Apr 09, 2006
Posts: 72


Here is the equals() method of String class.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9351
    
    2

Thanks Vinayagar.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: equals doubt??