There is hardly any Java Interview, where no questions is asked from String, and
Why String is Immutable in Java is I think most popular one. This question is also asked as
Why String class is made final in Java or simply,
Why String is final. In order to answer these questions, Java programmer must have a solid understanding of How String works, what are special features of this class and some key fundamentals. String is a God class in Java, It has got special features which is not available to other classes e.g.
String literals are stored in pools, You can concatenate strings using + operator. Given its importance in Java programming, Java designer has made it
final, which means you can not extend
java.lang.String class, this also helps to making
String object Immutable. Now coming to questions,
Why String is Immutable in Java? of course it should be related to benefits, advantages. Now let's think what are those advantages or features, which drives this decision. I don't know if there is any official document from Oracle or Sun previously, which can throw some light on this decision. Though I remember reading somewhere, that once asked to James Gosling, creator of Java about making String class final, he has said something along security. It's been argued that
making a class final seriously limits its ability to evolve or extend and James has made comment that, classes which are key to Java's Security commitment are made final, so that no one can change its behaviour and game with Java platform.