• Post Reply Bookmark Topic Watch Topic
  • New Topic

hashCode() and identity of an object  RSS feed

 
Vijaya Bhaskar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,

What exactly hashCode() returns?
I have gone through something like that "an object has its identity".

What exactly mean by an identity of an object?

Is it (identity) what hashCode() returns?

 
Tapas Chand
Ranch Hand
Posts: 614
9
BSD Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found a thread regarding hashcode in ranch itself.
Go though the link.
 
Vijaya Bhaskar
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Chand,

That is okay but little bit is required again on:

An object has 3 things:
                             1. state
                             2. behavior
                             3. identity



What do you mean by identity here?
 
Ganesh Patekar
Bartender
Posts: 726
23
Eclipse IDE Hibernate jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijaya Bhaskar wrote:What do you mean by identity here?
Perhaps this may help, read Defining Object Identity and this Old thread of explanation of Object Identity
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Vijaya Bhaskar wrote:What do you mean by identity here?

Something that makes an object distinct from other objects; and all objects in Java have at least one form of identity - their reference, which is similar to (and may actually be) a memory location - since no two objects can occupy the same space at the same time.

However, this is generally only useful to the JVM itself for managing memory. An application that you write that deals with people, for example, is likely to want some way of knowing when two different Person objects refer to the same person.

And this form of identity doesn't just exist in Java. You usually need an identity to store a person in a database, or to make sure that a bank or ATM doesn't give money to the wrong "John Smith".

And oddly enough, identifying people is one of the oldest problems in Information Management, because most of the normal methods we use (name, address, date of birth, etc) are insufficient to uniquely identify someone to a stranger or a computer system - although they're usually fine for social situations. And that's why our lives abound with customer IDs, account and social security numbers, and PINs.

Imagine I walked into a bank and said: "Hello, I'm John Smith. I'd like to take out £500 please". What's likely to be the response?
Assuming they don't laugh in my face, they might ask me all sorts of questions to try to whittle me down; but they'd still have no way of knowing whether or not I really was John Smith - I could simply be giving answers I read off a bank statement I found in a rubbish bin.

And the same (or similar) is true of many data structures - specifically Sets and Maps - you find in Java. They need to ensure that they don't contain "duplicate" objects (or keys); and the way they usually do that is with the equals() and hashCode() methods common to all Java objects - the latter only in the case of "hashed" datasets (which normally include 'Hash' somewhere in the class name).

And that's why it's so important to implement these methods for practically every class you write.

HIH

Winston
 
Campbell Ritchie
Marshal
Posts: 56518
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is quite unusual, as Winston says, to need to know that two references both point to the same object, and in a few cases, e.g. Strings, that can actually confuse one. It is very common to need to know that the data encapsulated in two objects are the same or different. If you have a photo of a black Maserati showing CR1 on the front registration number, and a photo of a black Maserati with CR1 on the back numberplate, you can be sure that
  • Both photos were taken of the same car.
  • Campbell has more money than sense
  • The two photos may be different, but they both show the same car. The equals() method is a way of showing that both photos show the same data (the same car) and the hashcode is a way of numerically sorting objects to make them quicker to find.
     
    Vijaya Bhaskar
    Greenhorn
    Posts: 28
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks to all replies.

    Cheers,
     
    Jesper de Jong
    Java Cowboy
    Sheriff
    Posts: 16057
    88
    Android IntelliJ IDE Java Scala Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Vijaya Bhaskar wrote:Is it (identity) what hashCode() returns?

    No, definitely not.

    Different objects can have the same hash code - the hash code alone is not a unique identifier of objects.
     
    Campbell Ritchie
    Marshal
    Posts: 56518
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Even though the documentation for Object#hashCode() suggests it uses memory location for the hash code, that does not mean you can get identity from the hash code, nor from System#identityHashCode(). Reasons:-
  • 1: It says that memory location is a typical implementation, but that might not be how all implementations calculate the hash code.
  • 2; After a round of garbage collection, the object may move to a new memory location, vacating the original location so another object is put onto it. You now have two different objects which return the same value from Object#hashCode/identityHashCode (since the old hash code mustn't change because of garbage collection). These two objects do not return true from equals(), maybe not if it is overridden. In fact they might not even be instances of the same class.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!