Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

equals and hashcode  RSS feed

 
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?
 
Sheriff
Posts: 21421
94
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.
 
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?
 
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
 
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
Boost this thread!