File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "about equals method?" Watch "about equals method?" New topic

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.

Keith Lynn
Ranch Hand

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

Joined: Nov 18, 2006
Posts: 689

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()

Amit Ghorpade

Joined: Jun 06, 2007
Posts: 2851

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

|Asking Good Questions|
An Sush
Ranch Hand

Joined: Jan 17, 2007
Posts: 47
If you open the class (inside 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

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

Joined: Oct 02, 2003
Posts: 11955


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.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
I agree. Here's the link:
subject: about equals method?
It's not a secret anymore!