简体   繁体   中英

Strings - How do they work in Java? Also, what does immutability mean?

String is immutable. See this code.

String a="ninja";

String b= "ninja";

String c=a+" name ";

In this a and b are pointing to same object. c will be another object. How it will become immutable when two strings are pointing to same object? Suppose if i modify a with another string like, a=" ninjaturtle"

then it will points to another place in heap. So can anyone explain in detail, when we can modify that object how can you say that its immutable?

String is immutable, you cannot change it. Assigning a = "ninjaturtle" will simply make a point to another string, it will not change the string b is pointing to.

You are mixing up 2 things 1) Reference variable -- Its similar to the pointers (not really, you cannot really do pointer operations) 2) Objects -- The actual objects which reference variable pointing to

String Object is a type of Object which you can never change the value once its created. Thats why its immutable. Instead, if you are considering StringBuilder object, you can change its value. Hence its a mutable object.

Going by your example:

String a="ninja";

a) when above statement is encountered,the JVM looks that whether a String Object with value "ninja" is already there in the String the String pool, if not then it creates a new String Object and stores it in String pool.

PLEASE NOTE: here 'a' is pointer to a String Object with value "ninja"

String b= "ninja";

b) When this statement is encountered again the JVM follows the same procedure as above and hence looks if some object already exists in the String pool with value "ninja". As we know that, we have already created such an object in step a). Now, compiler will not create a new object but, it will point 'b' to the same Object which has value "ninja"

String c=a+" name ";

c) This statement again follows same procedure as mentioned above and creates a new Object with value "ninja name "

after above three statement we have following in String pool:

a --------------------------> new String("ninja");

b ------------------------------^

c --------------------------> new String("ninja name ");

To give your answer let us try to modify 'a'.

a="ninja name ";

d) Now, compiler will first check the String pool, since, and Object with value "ninja name " already exists the pointer 'a' will now point to new String("ninja name ")'

As a result the String pool now has following String pool:

b --------------------------> new String("ninja");

c --------------------------> new String("ninja name ");

a ---------------------------------^

If you notice the String Objects in the String pool are still the same. When we tried to modify them then they only pointed to some other String Object in the pool or a new String was created but, existing String was never modified. This is immutability of String

immutable means that the physical string cannot be changed. This means that while I can change the location that a string points to, I cannot alter the string in any way.

That is why when you do something like

String a = "    hello world";
a.trim();
System.out.println(a); //Prints out:    hello world

there will still be 4 spaces before "hello world".

you must reassign the location, like so:

String a = "    hello world";
a = a.trim();
System.out.println(a); //Prints out:hello world

Strings don't change, memory locations do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM