public static void main(String[] args) {
System.out.println(new boolean[] {}.hashCode());
}
In normal run result is always 189568618
In debug result is always 321142942
In debug means "-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:50524,suspend=y,server=n"
Why?
(azul-21.0.2)
it is not IDE/Maven issue. It is commandline output (numbers are different than in IDE)
md5sum Ops.class
0cd9aa057204d816ea1a1edf4eb9f792 *Ops.class
java Ops
681842940
java Ops
681842940
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 Ops
Listening for transport dt_socket at address: 8000
1555845260
java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 Ops
Listening for transport dt_socket at address: 8000
1555845260
md5sum Ops.class
0cd9aa057204d816ea1a1edf4eb9f792 *Ops.class
According to the Java 11 javadocs1 for hashCode()
:
(The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)
In earlier versions of Java it typically was based on a memory address. In newer versions it typically IS NOT based on an address. I think that the current default is to use a PRNG. For more information2 see this:
So ... to understand precisely why >you< get different hashCode values for the same object, you would need to find out which of the alternative hashCode algorithms is being used in your JVM(s).
But either way, there is nothing in the javadoc to say that the identity hashcode of an object will be the same from one run to the next. What you are observing is an implementation artifact. It is something you shouldn't rely on.
1 - Other versions of the javadoc say different things. The latest versions remove all mention of how the hashCode is generated.
2 - It is possible that Aleksey's article is out of date. If it really matters to you, download the OpenJDK source code and do your own analysis.
3 - That's what Aleksey wrote on the page!