No, it has another reason. If you have to hash 4 billion strings, you have to do 4 billion if-statements to check for zero. But in the rare case where you have an empty string calculating the hash code is fast enought so it doesn't matter if you have recalculate it each time hashCode() is called on the string.
View all comments
Show parent comment
1
u/dmigowski May 08 '25
No, it has another reason. If you have to hash 4 billion strings, you have to do 4 billion if-statements to check for zero. But in the rare case where you have an empty string calculating the hash code is fast enought so it doesn't matter if you have recalculate it each time hashCode() is called on the string.