Java String, StringBuffer, StringBuilder

String is immutable. When you try to change the value of a String, it actually creates a new String object. StringBuffer and StringBuilder are mutable, while StringBuilder is thread-safe and StringBuffer is not.

BTW, String is not strictly immutable, one can change its value by the following way:

1
2
3
4
5
6
7
String s = "abc";

Field value = s.getClass().getDeclaredField("value");
value.setAccessible(true);
value.set(s, "abcd".toCharArray());

System.out.println(s); // abcd

Reference