Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes about equals method? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about equals method?" Watch "about equals method?" New topic
Author

about equals method?

vijay mahendra
Ranch Hand

Joined: Jan 01, 2007
Posts: 51
hi one and all,

object1 and object2 are type of same object references

if object1.equals(object2) returns false

why its returning false value. i need reason behind that .

please any one give suggesions.

thanks
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
It depends on the class definition. If a class definition doesn't override the equals(Object) method from Object.java, then it inherits the equals(Object) method from Object.java. That method simply tests whether the references refer to the same object.
Kaydell Leavitt
Ranch Hand

Joined: Nov 18, 2006
Posts: 688

obj1 and obj2 may be referenced by the same type of object reference variable, but they might be different types of objects. If equals() was overriden properly, sometimes obj1 and obj2 are of different classes:

obj1.class() might not be equal to obj2.class()

Kaydell
Amit Ghorpade
Bartender

Joined: Jun 06, 2007
Posts: 2716
    
    6

Hi,
the above posts describe exactly what is happening behind the scenes.
So the best way to check the equality of your custom objects is to override the .equals() method of the Object class and define the way in which you think that your objects/ object contents are equal.

Hope this helps


SCJP, SCWCD.
|Asking Good Questions|
An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
If you open the class Object.java (inside src.zip of your jdk folder) which is superclass of all class, you will find the implementation of equals method and its like



So you can clearly see that the default implementation (in case you don�t define equals method in your class) is object reference equality and not value equality. If you want to have equals method work in your Object, you must override the implementation in your object.
You can refer what they have done in String.java


So based on what your object it and how you want to define your equality, you must override the method in your class

Hope it�s helpful
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11170
    
  16

"Sushant",

You have apparently missed our Naming Policy on the way in. We require a real first name, followed by a space, and a last name. Please follow that link, read the page, and find the link there on how to correct your screen name. This policy is mandatory.

I would have contacted you privatly, but you did not register with an email.

Thanks!


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: about equals method?
 
Similar Threads
KHALIDS book-CONTRADICTION on GC
JComboBox Selection?
DOUBT REGARDING OBJECT REFERENCES
equals() method and == operator
null question