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