I've been studying Java String for a while. The following questions are based on the below posts
Java String is special
Immutability of String in java
Immutability: Now, going by the immutability, the String class has been designed so that the values in the common pool can be reused in other places/variables. This holds good if the
String
was created asString a = "Hello World!";
However, if I create String likeString b = new String("Hello World!");
why is this immutable as well? (or is it?). Since this has a dedicated heap memory, I should be able to modify this without affecting any other variable. So by design, was there any other reason whyString
as a whole is considered immutable? Or is my above assumption wrong?Second thing I wanted to ask was about the common string pool. If I create a string object as
String c = "";
is an empty entry created in the pool?
Is there any post already on these? If so, could someone share the link?
String is immutable because it does not provide you with a mean to modify it. It is design to avoid any tampering (it is final, the underlying array is not supposed to be touched ...).
Identically, Integer is immutable, because there is no way to modify it.
It does not matter how you create it.
1-String is immutable.See this:
Is a Java string really immutable?
So you can create it with many way.
2-Short answer: Yes will be empty.
Now String
B called
"Test".toUpperCase()which change the same object into
"TEST", so
Awill also be
"TEST"` which is not desirable.String is immutable means that you cannot change the object itself no matter how you created it.And as for the second question: yes it will create an entry.
new String()
is an expression that produces aString
... and aString
is immutable, no matter how it is produced.(Asking if
new String()
is mutable or not is nonsensical. It is program code, not a value. But I take it that that is not what you really meant.)Yes; that is, an entry is created for the empty string. There is nothing special about an empty
String
.(To be pedantic, the pool entry for
""
gets created long before your code is executed. In fact, it is created when your code is loaded ... or possibly even earlier than that.)Yes it is. But the immutability is a fundamental property of String objects. All
String
objects.You see, the
String
API simply does not provide any methods for changing aString
. So (apart from some dangerous and foolish1 tricks using reflection), you can't mutate aString
.The reason that Java
String
is designed as an immutable class is simplicity. It makes it easier to write correct programs, and read / reason about other people's code if the core string class provides an immutable interface. (Or at least, that is the rationale for this design decision, as I understand it.)No. It is more fundamental than that. Simply, all
String
objects are immutable. There is no complicated special case reasoning required to understand this. It just >>is<<.For the record, if you want a mutable "string-like" object in Java, you can use
StringBuilder
orStringBuffer
. But these are different types to String.1 - The reason these tricks are (IMO) dangerous and foolish is that they affect the values of strings that are potentially shared by other parts of your application via the string pool. This can cause chaos ... in ways that the next guy maintaining your code has little chance of tracking down.
String is immutable irrespective of how it is instantiated
1) Short answer is yes,
new String()
is immutable too.Because every possible mutable operation (like
replace
,toLowerCase
etcetra) that you perform onString
does not affect the originalString
instance and returns you a new instance.You may check this in Javadoc for
String
. Eachpublic
method ofString
that is exposed returns a newString
instance and does not alter the present instance on which you called the method.This is very helpful in Multi-threaded environment as you don't have to think about mutability (someone will change the value) every time you pass or share the
String
around.String
can easily be the most used data type, so the designers have blessed us all to not think about mutability everytime and saved us a lot of pain.Immutability allowed String pool or caching
It is because of immutability property that the internal pool of string was possible, as when same String value is required at some other place then that immutable reference is returned. If
String
would have been mutable then it would not have been possible to shareString
s like this to save memory.String immutablity was not because of pooling, but immutability has more benefits attached to it.
String interning or pooling is an example of Flyweight Design pattern
2) Yes it will be interned like any other
String
as a blankString
is also as much aString
as otherString
instances.References: