The Curious Case Of Strings In Java
img src The most important thing to note about String objects in java is that they are immutable. Immutability means that once the state of an object is defined, it cannot be changed. To make String immutable, all the attributes in String class are declared as final. The attribute 'hash' holding the hashcode is an exception though. Following extract from the String class shows the declaration of the attribute which holds the String's value 1 2 /** The value is used for character storage. */ private final char value []; Because this char array is declared as final, once a String object is assigned a value, it can never be changed. However, the references to a String object are mutable, which means , we can point to another String object using the same reference. Let's look at the following code which explains the above concept : 1 2 3 4 5 6 7 8 9 10 11 12 13 public class TestStrings { public static void ...