5

I continuously run this program on the same machine:

class Test {
    public static void main(String[] args) {
        Test test = new Test();
        System.out.println(test.hashCode());
    }
}

The result is the same each time I run the program on my machine (Windows 7 64 bit): 4384790

On another machine (Windows server 2008, 64 bit), most times it gives me :1671711. However some times the result is : 11394033.

On my machines I run the program under Java HotSpot(TM) Client VM 1.6.0_26/1.6.0_33

I have read the documentation for Object class:

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 JavaTM programming language.)

But if hashCode() is indeed implemented by converting the internal address of the object into an integer, I still do not understand why the Java VM assigns the same address for the Test object each time I run it.

Is is known exactly how the default hashCode() operates?

Alex
  • 971
  • 1
  • 9
  • 23
  • It is very carefully *not* specified, and as you have discovered it varies by platform. – user207421 Mar 06 '13 at 09:09
  • I *think* the "internal address" is not the physical address in RAM, but something internal to the JVM, which probably starts at the same "logical" value each time - therefor if you always create the same object in the same class it would always wind up as the "first" one based on the "logical" address-scheme (again: I'm just speculating) –  Mar 06 '13 at 09:11
  • @Perception: The accepted answer to that other question does not apply to modern JVMs. Please see my answer below for an explanation (in a nutshell, the object's address is not used *at all* by `Object.hashCode()`). – NPE Mar 06 '13 at 09:17

2 Answers2

5

The details are JVM-specific. Furthermore, some (most?) JVMs support multiple hashCode() algorithms (when starting the JVM, you can choose which one it would use).

Interestingly, most of those algorithms -- including Oracle's defaults -- do not use the object's address at all. For example, JDK7's default hashCode() uses a global pseudo-random number generator, and then caches the generated pseudo-random hash code within the object's header.

Of course, a psuedo-random generator would always give the same sequence of numbers if initialized the same way.

For the details of an investigation I've done in the past, see What is an "internal address" in Java?

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
3

Non-kernel programs never work with absolute memory addresses, they use virtual memory where each process gets its own address space. So a program would place data at the same location in each run.

  • True as it may be, this has no bearing on `Object.hashCode()` (at least in modern Oracle JVMs). See my answer for details. – NPE Mar 06 '13 at 09:26