Consistency of hashCode() on a Java string

I can see that documentation as far back as Java 1.2.

While it’s true that in general you shouldn’t rely on a hash code implementation remaining the same, it’s now documented behaviour for java.lang.String, so changing it would count as breaking existing contracts.

Wherever possible, you shouldn’t rely on hash codes staying the same across versions etc – but in my mind java.lang.String is a special case simply because the algorithm has been specified… so long as you’re willing to abandon compatibility with releases before the algorithm was specified, of course.

Leave a Comment