This answer gives an indication that class String is declared final for thread safety, which does not convince.
This answer does not convince when it says: And so, you make the classes final. There can be no mutable String, because its an immutable class and its final.
Because,
below field with final modifier,
/** The value is used for character storage. */
private final char value[];
in class String would suffice/indicate that data stored in the created object is suppose to be immutable and thread safe.
So, class String being final has nothing to do with immutability or thread safety.
But it is not clear, why class String is declared final?
Because, as per below class definition, it has final modifier:
public final class String
implements java.io.Serializable, Comparable<String>, CharSequence
{ .... }
So, class String cannot be inherited.
Despite field value[] in class String is declared final, additionaly, What is the necessity for class String also to be final?
Note: Answers to this question will give an idea behind this design decision
class Stringbeingfinalhas nothing to do with immutability. But this answer says here:Another reason is thread safety: Immutables are always thread safe. I think thread safety can be ensured by just making the field ofclass Stringasfinal, but notclass Stringasfinal? 2) Similarly this answer does not make sense to me:And so, you make the ..an immutable class and its final.finalsaves programmer from the need to repeat declaringfinalin each and every method declaration, then why required methods are declaredfinalinpublic final class String{...}? – overexchange Jun 17 '15 at 08:10String-like class - however, why not just make aStringinterface and then an implementingImmutableStringclass. So, everyone who cares for immutability e.g. for security or performance reasons can just expect its caller to deliver anImmutableString. Everyone else can just accept a String. Why hasn't it been done this way? – valenterry Jun 17 '15 at 09:14there are reasons, please let me know, what are those? – overexchange Jun 17 '15 at 09:55Stringand he wants to make sure that he knows in before, how its methods are implemented. E.g. he wants to assure that "abc".startsWith("abc") is true and not false because someone has overwritten the method and is not passing aStringbut aCustomStringclass. – valenterry Jun 17 '15 at 10:46Stringfinal to achieve this. Their mistake was probably not to make an interface forString. – valenterry Jun 18 '15 at 14:54