News for hackers. Stuff that matters.
Re:Java 7 changed the structure of String by abollaert on Friday July 10, 2015 @03:57AM (#9862776)
I don't think he refers to the caching being a bad thing, but that it probably does not affect performance that much, since most Strings are likely to be small (and the number of times hashcode is called is also likely to be small).

There's also a catch : if the hashcode of the string is 0, the hashcode will be recalculated every time (since the code assumes it has not been cached yet).

Re:Java 7 changed the structure of String by moonchrome on Friday July 10, 2015 @04:14AM (#9862811)
>There's also a catch : if the hashcode of the string is 0, the hashcode will be recalculated every time (since the code assumes it has not been cached yet).

At least that part should be easy to fix by defining a hash function that returns numbers != 0 - even the article says they did it for JVM7 but it's gone in JVM8 - with no explanation ?

Re:Java 7 changed the structure of String by Ironlink on Friday July 10, 2015 @04:50AM (#9862901)
The hash32 implementation in Java 7 was not intended to fix the case where the actual hash value was zero, nor did it have an impact on that case as the hash32 value was stored in a separate field.

It was made to decrease the number of hash value collisions in large data structures (hash maps and such). Its replacement is described here: http://openjdk.java.net/jeps/180 . Given that most Strings never end up in a large collection, allocating an extra 4 bytes for every one of them was a waste.

This post has been edited once for factual correctness.

Re:Java 7 changed the structure of String by mtdewcmu on Friday July 10, 2015 @05:58AM (#9863077)
It's hard to imagine a situation where the hash function would be so slow as to justify adding 4 bytes to every string.
Re:Java 7 changed the structure of String by joosters on Friday July 10, 2015 @06:47AM (#9863187)
If your strings are large, then calculating the hash will take time AND the extra 4 bytes for hash storage will be minimal extra overhead.

You can easily find good & bad cases for all of these string implementations. They all have tradeoffs.

Re:Java 7 changed the structure of String by brandonbloom on Friday July 10, 2015 @04:14AM (#9862812)
Can just bit-or 1.
Re:Java 7 changed the structure of String by wolf550e on Friday July 10, 2015 @05:03AM (#9862941)
this halves the number of values. doing if (hashcode == 0) hashcode = 1; removes only one value.