Domanda

My textbook says a String is not over-writable or immutable, i.e, once you enter the value of a String you can't change it. But today when I was running the following code, the String str gets muted as the compiler does not give any error and the new String a's value is successfully entered into str.

class Test
{
static void main()
{
    String str = "something";
    String a ="anything";
    str = a; //str is being over written without any error
    System.out.println(str);
}
}

The output is : anything

So, is my book wrong ?

If my book is not wrong please give an example to show that Strings are immutable

È stato utile?

Soluzione

The book is correct. When you say str = a you are not changing anything about the String 'something'. You should distinguish between str and something, they are not the same. "something" here is a String object in memory, whereas str is just the reference to that string. Same with the reference a.

When you say:

str = a

You are not changing something, you are in fact saying, "change the reference str to point to whatever the reference a is pointing to." The Strings remain the same, the references change.

On a similar note, this is why you may see in your book that concatenating Strings is expensive, as doing something like:

str = str + a

Would again not be changing the existing Strings, but instead creating a new String object which is equal to the concatenation of the String that the reference str is referring to and the String that the reference a is referring to.

Altri suggerimenti

You need to understand what immutable means. In your scenario you are just changing references.

str = a;

will make both a and str to point to String "anything". The Text Book is correct. String is immutable and can not be overwritten. If you check the JavaDoc for String. Most of the methods return a String. This is because any operation in a String will not change that String object but will result in a new String being created. Effectively you can never change a String after you create it. By Change I mean append new characters, remove characters without a new String object being created.

As many answers already point out is that you only change references. Immutable means you cannot change the string itself. for example you do:

String a = "anything";
System.out.println(a); // -> anything

a.substring(3);
System.out.println(a); // -> anything : this is because the String itself is 
                      //               immutable.

a = a.substring(3);
System.out.println(a); // -> thing : this is what immutable means to edit a string 
                      //            you must reassign it or assign it 
                      //            to a new variable

You're changing the reference of str to a. So str effectively becomes a's value.

they are just pointer to that string. so when you do str =a , you just assign pointer of a to str.

The contents of the String object is not being changed. What's happening is that a new String object is being assigned to the variable. The old String object still exists in memory but you just can't refer to it any more. The 'str' variable now refers to the String object containing "anything" but the String containing "something" still exists as it did before. Try assigning 'str' to another variable first and then, after assigning 'a' to 'str', check that other variable and you'll see that it still says "something", proving that that String was not overwritten.

In Java, the value of a variable is never an object, but a reference. Relevant to your case, the type String on the a variable says the variable is allowed to contain only references to String objects.

You can update the value in the variable, sure; but that won't touch the object it is referring to.

Strings are constant; their values cannot be changed after they are created.

The String object is created and stored on constant pool or literal pool.

In your case, when you say,

String str="something"; // An object is created on constant pool with value 'something' and reference 'str' is pointing to it.

String a="anything"; // An object is created on constant pool with value 'anything' and reference 'a' is pointing to it.

And when you do, str=a; then 'str' actually start pointed to 'a', but the object with value as 'something' remains on constant pool with the same value.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top