• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

hashCode() and identity of an object

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 624
9
BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1251
87
Hibernate jQuery Spring MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks to all replies.

    Cheers,
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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: 79180
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
  •  
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic