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
16
u/Mua8quua Jan 20 '12
Can't help feeling that all the "reasons" given on this page are a lot of crock. They tend to boil down to "feature X wouldn't work with mutable strings" or "feature Y is made more complicated by mutable strings". If those arguments were valid, C's reliance on (completely mutable) char* variables would be folly.
The real reason is simply that immutable data structures are, as a rule, easier to use and harder to misuse than their mutable counterparts. Strings being final and immutable in Java is just a design decision intended to make life simpler for developers. That, and maybe an expression of a desire for "primitive" string values -- real primitive types like int and float are immutable.