Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mutable strings in Java

As almost everybody knows strings in Java are immutable. Recently I discovered something that might suggest that it's not always true. Let's try out this code:

System.out.println("-------- BEFORE MODIFICATIONS --------");
String beforeTest = new String("Original");
System.out.println(beforeTest);
java.lang.reflect.Field valueField = String.class.getDeclaredField("value");
valueField.setAccessible(true);
valueField.set("Original", "Modified".toCharArray());
System.out.println("-------- AFTER MODIFICATIONS --------");
System.out.println(beforeTest);
System.out.println("Original");
String test = new String("Original");
System.out.println(test);
String test2 = new String("Original 2");
System.out.println(test2);

the output would be:

-------- BEFORE MODIFICATIONS --------
Original
-------- AFTER MODIFICATIONS --------
Original
Modified
Modified
Original 2

How does this trick work? How does the JVM know which objects should be changed and which not? What mechanism are under the hood of this trick? Why already created beforeTest string was not changed? Does this trick really derogate the strings are immutable principle?

like image 312
Adam Sznajder Avatar asked Jun 22 '12 21:06

Adam Sznajder


People also ask

What is mutable and immutable strings in Java?

The mutable objects can be changed to any value or state without adding a new object. Whereas, the immutable objects can not be changed to its value or state once it is created. In the case of immutable objects, whenever we change the state of the object, a new object will be created.

Why string is mutable in Java with example?

In the String constant pool, a String object is likely to have one or many references. If several references point to the same String without even knowing it, it would be bad if one of the references modified that String value. That's why String objects are immutable.

Is string mutable or immutable in Java?

String is an example of an immutable type. A String object always represents the same string. StringBuilder is an example of a mutable type.

What are immutable strings in Java?

The string is immutable means that we cannot change the object itself, but we can change the reference to the object. The string is made final to not allow others to extend it and destroy its immutability.


1 Answers

String literals are interned into a pool. This means that when you write

String s1 = "Foo";
String s2 = "Foo";
String s3 = new String("Foo");

s1 and s2 refer to the same String object, and s3 refers to another one, backed by another char array.

In your code, you violate String's invariants by modifying the private char array holding the characters of the "Original" String literal instance. But since beforeTest refers to another String instance, it's not modified.

Immutability is achieved by keeping fields private into an object, and not providing any method to modify this private state. By using reflection, you break all the rules of encapsulation, and you can thus violate immutability.

like image 79
JB Nizet Avatar answered Sep 19 '22 19:09

JB Nizet