Is "new String()" immutable as well?
1) The immutable part isn't because of the pool; it just makes the pool possible in the first place. Strings are often passed as arguments to other functions or even shared with other threads; Making Strings immutable was a design decision to make reasoning in such situations easier. So yes - String
s in Java are always immutable, no matter how you create them (note that it's possible to have mutable strings in java - just not with the String
class).
2) Yes. Probably. I'm not actually 100% sure, but that should be the case.
new String()
is an expression that produces a String
... and a String
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.)
If I create a string object as
String c = "";
is an empty entry created in the pool?
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.)
So, I was wanted to know whether the new heap object is immutable as well, ...
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 a String
. So (apart from some dangerous and foolish1 tricks using reflection), you can't mutate a String
.
and if so what was the purpose?.
The primary 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.
An important second reason is that the immutability of String
has fundamental implications for the Java security model. But I don't think this was a driver in the original language design ... in Java 1.0 and earlier.
Going by the answer, I gather that other references to the same variable is one of the reasons. Please let me know if I am right in understanding this.
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
or StringBuffer
. 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.
The Java libraries are heavily optimized around the constraint that any String
object is immutable, regardless of how that object is constructed. Even if you create your b
using new
, other code that you pass that instance to will treat the value as immutable. This is an example of the Value Object pattern, and all of the advantages (thread-safety, no need to make private copies) apply.
The empty string ""
is a legitimate String
object just like anything else, it just happens to have no internal contents, and since all compile-time constant strings are interned, I'll virtually guarantee that some runtime library has already caused it to be added to the pool.
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 on String
does not affect the original String
instance and returns you a new instance.
You may check this in Javadoc for String
. Each public
method of String
that is exposed returns a new String
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 share String
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 blank String
is also as much a String
as other String
instances.
References:
- Immutability benefits of String