• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Doubt: equals() & hashcode() method

 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have five questions to which i am hoping to get an answer.

1] What does the default implementation of equals() method do?

2] What does it do when overridden?

3] What does the default implementation of hashcode() method do?

4] What does it do when overridden?

5] How do we define what is the appropriate, correct, legal implementation of these methods?

I have already tried searching in google , but found all the answers verbose , i hope these questions can help others who too are confused about these methods.

Thanks,
Piya
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

piya rai wrote:1] What does the default implementation of equals() method do?



Well the equals method of Object class checks for reference equality i.e. it checks if two references point to the same object. So basically it does a == check on the objects.

piya rai wrote:2] What does it do when overridden?



What do you mean. You decide what you do with it when you override it. The general rules are that you follow the hashCode and equals contract.

piya rai wrote:3] What does the default implementation of hashcode() method do?



The default implementation usually generates distinct hash codes for different objects.

piya rai wrote:4] What does it do when overridden?



Again here, it depends on what you want to do with it.

piya rai wrote:5] How do we define what is the appropriate, correct, legal implementation of these methods?



I think you should read the hashCode and equals documentation to get some good idea. That will solve your other questions too...
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have tried to answer the questions.

1] What does the default implementation of equals() method do?
It checks the Object equality.
i.e.
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).

2] What does it do when overridden?

It behaves as per the code you write in the overridden method.
e.g. if you have student class, you can just check the roll number of two student objects to declare that they are equal.
There are defined rules for equality check that it should be reflexive,symmetric,transitive & consistent

3] What does the default implementation of hashcode() method do?
Returns a hash code value (an integer) for the object.
This method is supported for the benefit of hashtables to identify the proper storage bucket.

4] What does it do when overridden?
Again depends on the code you write in the overridden method.
You have to define the implementation for your user defined class that follows the general contract for hashcode.
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html

5] How do we define what is the appropriate, correct, legal implementation of these methods?
There are guidelines in the API
http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html

 
Sometimes you feel like a nut. Sometimes you feel like a tiny ad.
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic