13

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?

Chan
  • 2,601
  • 6
  • 28
  • 45
Adam Sznajder
  • 9,108
  • 4
  • 39
  • 60

1 Answers1

19

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • Isn't third instance (test reference) also pointing to new instance? If "Original" literal char is replaced by "Modified" shouldn't beforeTest value be updated (because same literal being passed for instance also)? – kosa Jun 22 '12 at 22:14
  • The `test` variable is initialized to a copy of the "Original" String literal. But you have alread modified its content to "Modified" when the copy is made. So it's a copy of "Modified". `beforeTest` is also a copy of the "Original" String literal, but the copy has been made before you modified its content. – JB Nizet Jun 22 '12 at 22:16