It make sense to make class String
objects immutable with following declarations, because they share the same storage in common pool.
String str1 = "abc";
String str2 = "abc";
But, am not sure, if it make sense to have class String
objects immutable with following explicit constructor declarations, because they do not share the same storage in heap.
String str3 = new String("def");
String str4 = new String("def");
So, if the content of a class String
object has to be modified frequently, it is recommended to learn class StringBuffer
and class StringBuilder
.
Does it make sense to know, What is the reason that class String
has been designed to maintain immutable content with an explicit constructor?