That's not true, jvm will do the best to return well distribute integers, so that it is very unlikely for two objects considered different (according to equals() method) to have the same hashcode, but it's not impossible.Object class's hashCode method returns unique hashcode for every object
If you let this program run for some time
Code: Select all
Object tmp,target = new Object();
int targetHash = target.hashCode();
do tmp = new Object();
while (tmp.hashCode() != targetHash);
System.out.println("tmp.equals(target) = "+tmp.equals(target));
System.out.println("tmp.hashCode() = "+tmp.hashCode());
System.out.println("target.hashCode() = "+target.hashCode());
Code: Select all
tmp.equals(target) = false
tmp.hashCode() = 1704856573
target.hashCode() = 1704856573