Java – why does hashcode () return the same value for an object in all successive executions?
I try some code about object equality in Java As I read somewhere
Now I have a sample program that I run 10 times in a row Every time I run the program, I get the same value as the hash code
If the hashcode () function returns the memory location of the object, how can Java (JVM) store the object at the same memory address in continuous operation?
Can you give me some insight and your views on this problem?
The program I'm running to test this behavior is as follows:
public class EqualityIndex { private int index; public EqualityIndex(int initialIndex) { this.index = initialIndex; } public static void main(String[] args) { EqualityIndex ei = new EqualityIndex(2); System.out.println(ei.hashCode()); } }
Each time you run this program, the hash code value returned is 4072869
Solution
Why not? Non kernel programs never use absolute memory addresses. They use virtual memory. Each process has its own address space So there is no doubt that a deterministic program will put things in the same place every time it runs