Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

equals and hashcode

 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20510
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you familiar with the hashCode "contract"? See the API documentation for hashCode() in java.lang.Object.
 
Rajiv Chelsea
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).


 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic