Hello,
Oracle's documentation for hashCode asserts:
Whenever it is invoked on the same object more than once during an execution of
a Java application, the hashCode method must consistently return the same
integer, provided no information used in equals comparisons on the object is
modified. This integer need not remain consistent from one execution of an
application to another execution of the same application.
It would be helpful if I could rely on hashCode always to return the same
integer for the same object.
The documentation implies that I cannot rely on the function in this way. I
have tested it several times, and the same integers are returned every time,
between separate runs of the application, after exiting and restarting
NetBeans, and after rebooting the computer. But it's not possible to do
hundreds of tests, so I'm asking if anyone knows enough about this method to
say that it will always return consistent values.
Here is the code I used for testing (I am only concerned with calls to hashCode
on Strings):
public static void main(String[] args) {
String s1 = "asddh:2 qwert:3 zxcv:4";
String s2 = "poiuy:19 lkjhg:22 mnbvc:27 jghfkd:16";
String s3 = "lakdnofgneoqwinelkvnlzjkndoafoibvklaznbdslvnvlaksv";
System.out.println(s1.hashCode()+" "+s2.hashCode()+" "+s3.hashCode());
}
Thanks very much.
Chris Lanz
Department of Computer Science
340 Dunn Hall, SUNY Potsdam
[email protected]
315 268 1547