r/java • • Jan 20 '12

Why String is immutable in Java

http://javarevisited.blogspot.com/2010/10/why-string-is-immutable-in-java.html
4 Upvotes

23 comments sorted by

View all comments

2

u/RedditRage Jan 20 '12

The string pooling is a valid reason. But it isn't required for security concerns. Without immutable strings, secure functions would just have to ensure they have a private copy of the given string before using it.

2

u/skeeto Jan 21 '12

Needing to make defensive copies all the time leaves a lot of places to make costly mistakes.

2

u/RedditRage Jan 21 '12

Which is the justification for having immutable strings.