The Architectural Immutability of Strings:
In Java, strings are represented by the java.lang.String class. Unlike primitive characters, a String object is immutable—its character array cannot be modified, resized, or overwritten after creation. Any method that appears to modify a String (such as concat(), replace(), or toUpperCase()) does not alter the original instance; it allocates and returns a completely new String object on the Heap.
String Constant Pool (SCP) vs Standard Heap:
To conserve RAM, the JVM manages a specialized cache in Heap memory called the String Constant Pool (SCP):
- Literal Creation (
String s1 = "INDIA";): The JVM checks the SCP. If"INDIA"already exists, no new memory is allocated;s1simply receives the reference to the existing pooled instance. - Keyword Creation (
String s2 = new String("INDIA");): Thenewoperator bypasses the SCP check and forcibly instantiates an independent, distinct object in regular Heap memory.