Is "new String()" immutable as well?

immutability, java, string

Solution

`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.

Problem

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 as `String a = "Hello World!";` However, if I create String like `String 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 why `String` 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?

Original source

Related problems