File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes equals and hashcode Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "equals and hashcode" Watch "equals and hashcode" New topic
Author

equals and hashcode

Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
1) When will 2 objects of same class pass the "equals" test?

2) Can two objects of same or different classes have the same hashcode?
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19693
    
  20

Rajiv Chelsea wrote:1) When will 2 objects of same class pass the "equals" test?

It depends on how the equals method is written. For Object == is used, for String all characters must be equal, etc.

2) Can two objects of same or different classes have the same hashcode?

hashCode() returns an int. That means that there are only 2^32 = 4,294,967,296 different values possible. Eventually there will be a clash, where two unequal objects will have the same hash code. Fortunately, that's allowed.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Are you familiar with the hashCode "contract"? See the API documentation for hashCode() in java.lang.Object.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Rajiv Chelsea
Ranch Hand

Joined: Jun 15, 2010
Posts: 88
If a class doesnt override equals method of Object class, then in what case two objects of that class
pass the equals test? Will it pass if the values of the instance variables of both the objects
are equal?
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rajiv Chelsea wrote:If a class doesnt override equals method of Object class, then in what case two objects of that class
pass the equals test?

there is no case. ie, it cant pass the equals test

Rajiv Chelsea wrote:Will it pass if the values of the instance variables of both the objects are equal?


No.what do you mean by equal here ? object's value ? two motor bike can be similar, still both are different by its registration(unique) number .

I suggest you to *go through the equals method of Object source code*

hth
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Rajiv Chelsea wrote:If a class doesnt override equals method of Object class, then in what case two objects of that class
pass the equals test? Will it pass if the values of the instance variables of both the objects
are equal?

If you don't override equals, the default behaviour of the Object class will be used.
API wrote: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).




[My Blog]
All roads lead to JavaRanch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: equals and hashcode