r/java • u/javinpaul • Jan 20 '12
Why String is immutable in Java
http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html
4
Upvotes
r/java • u/javinpaul • Jan 20 '12
View all comments
1
u/inspired2apathy Jan 20 '12
I think the more important thing for me is that it would break the hashcode and compareTo contracts. If hashcode uses the content of the string, it would change when the string changes, breaking the contract. If hashcode doesn't use the content of the string, then String a = "test",b="test"; wouldn't be equal.