I figured out here with the lines and read the following thesis on one resource:

Since the string is immutable, its hashcode is cached at the time of creation and there is no need to count it again.

I got to java.lang.String and saw that at least the hashCode() method is there, but in the code itself this method is not called anywhere. I understand correctly that, in fact, when creating strings, the hash is not used and the thesis at the beginning is incorrect?

  • 3
    meant “hashcode is cached at the time of [ hashcode ] creation” - etki

1 answer 1

The thesis is true.
The hashcode value is calculated at the time of the first hashCode calls and is stored in the object field. Moreover, a method can calculate a value several times from different streams, since no synchronization for this field is provided.

  • one
    Taki thesis is true, it's just not about creating a string. - user236014
  • @Artem Konovalov: "from different streams" will the hashcode for one object always be the same? - TimurVI
  • @TimurVI yes, it is calculated based on the array of characters that make up the string, emnip - etki
  • @Anon is an ambiguous statement and can be interpreted both at the time of creating the string and at the time of calculating the value. And if we are talking about the calculation, then how can we say about the value that it was created? - Artem Konovalov
  • In general, changed to true, but not the essence. From the explanation to the answer I think, and so everything is clear. - Artem Konovalov