0

Possible Duplicate:
what is an objects hashcode

When i run this sample program

public class ZiggyTest {
    public static void main (String[] args){
        Object now = new Object();
        System.out.println(now);
    }
}

The output is

java.lang.Object@3e25a5

I understand that the number 3e25a5 is the hashcode value that represents the object. Where and how is this calculated/derived?

Community
  • 1
  • 1
ziggy
  • 15,677
  • 67
  • 194
  • 287

2 Answers2

1

According to the documentation:

As much as is reasonably practical, the hashCode method defined by class Object does return distinct integers for distinct objects. (This is typically implemented by converting the internal address of the object into an integer, but this implementation technique is not required by the Java programming language.)

Note that accordingly, the exact formula is not defined by Java and can vary by JVM implementation and JVM version.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
1

There is no official hashCode function. An implementations is allowed to create whatever hashCode makes sense for that implementation. Heck, the function could be different for different types if that makes sense. If you are using Eclipse, go to "Source-->Generate hashCode() and equals()" from the menu, and explore how you can configure the hashCode function. Basically you decide what declared members you want to use, if any, in building up a hashCode for this class. Then it patches things together with some prime numbers thrown in. It's an interesting approach.

Steve J
  • 2,676
  • 20
  • 18