State of now, the hashCode of a String is the same in between different
executions for (most) oracle vms.
That might change in the future and might not be correct for all vms out
there.

If you you want to go sure and be future safe, the only valid way I see, is
to create a utility method created in a class controlled by you, which will
return a stable int for a given string each time you invoke it.

Java itself cannot promise such a behavior, as an object created in one
execution is *NOT *the same as the object of another execution. Even not
for strings.

Am Do., 6. Mai 2021 um 13:38 Uhr schrieb Christopher C. Lanz
<lan...@potsdam.edu.invalid>:

> 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
>
> lan...@potsdam.edu
> 315 268 1547
>
>
>
>
>

Reply via email to