• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

equals method

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everybody,
Can anyone explains the result of this code :

public class Reference {
public static void main (String Args [] ){
Reference t1 = new Reference ();
Reference t2 = new Reference ();

if (t1.equals(t2)){
System.out.println ("Same content");
}else{
System.out.println ("Different content");
}
}
}
the output is : Different content. I thought it would be Same content.
Thank you
 
author
Posts: 23928
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

the output is : Different content. I thought it would be Same content.



Well, since your "Reference" class doesn't override the equals() method, you are using the equals() method implementation from the Object class. The implementation of the equals() method of the Object class returns true only if the two objects are the exact same instance.

Henry
 
Ranch Hand
Posts: 1710
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Moukoss,



If you dont override the equals() method that you have inherited from the Object class, you would get this result. Object class equals() method does nothing but returns true if references to the two objects are same. In your case you created two objects and references to both objects are different so they are different, and equals returns false.

Try this code if you want result true if you use equals to compare objects of class Reference:



But you would often want to give your equals method a deeper meaning that what I have done.

Hope this helps you!
Thanks and Regards,
cmbhatt
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i agree the upstairs .
check the j2se api:

The equals method for class Object implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference values x and y, this method returns true if and only if x and y refer to the same object (x == y has the value true).

 
Armel Moukoss
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you to everyone for your answer. I thought that the equals method was the same for Object, Integer,.. and so one. But i was wrong. thank you !!!
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic