In Java, String objects are immutable, meaning once a String is created, its value cannot be changed. Any operation that appears to modify a String actually creates a new String object.
When you perform operations like concatenation, replacement, or case conversion, Java does not alter the original String. Instead, it creates a new one.
// Demonstrating immutability of String
String s1 = "Java";
s1.concat(" Programming");
System.out.println(s1);
Java
The original String s1 remains unchanged because concat() creates a new String but does not assign it back to s1.
// Assigning the new String reference
String s1 = "Java";
s1 = s1.concat(" Programming");
System.out.println(s1);
Here, the new String created by concat() is assigned back to s1, so the updated value is displayed.
Enter a string and click an operation. Notice how the Original object remains in memory, and a completely New Object is created with a different memory address.
StringBuilder or StringBuffer for frequent modificationstoUpperCase()