Why Java Strings are Immutable? Last Updated : 10 Oct, 2025 Suggest changes Share 177 Likes Like Report In Java, strings are immutable, meaning their values cannot be changed once created. If you try to modify a string (e.g., using concat() or replace()), a new string object is created instead of altering the original one.Strings are stored in a String Pool, allowing reuse of objects and reducing memory overhead.Multiple threads can safely share the same string object without synchronization.Immutable strings have a consistent hash code, making them reliable for use in collections like HashMap.Example Demonstrating Immutability Java public class GFG { public static void main(String[] args) { // Both s1 and s2 refer to the same // string literal in the String Pool String s1 = "Hello"; String s2 = "Hello"; // true, both point to the same object in String Pool System.out.println("s1 == s2: " + (s1 == s2)); // Concatenation creates a new String // object in heap, s1 now points to it s1 = s1.concat(" World"); System.out.println("s1: " + s1); System.out.println("s2: " + s2); System.out.println("s1 == s2: " + (s1 == s2)); // Creating a string using new keyword stores it in the heap String s3 = new String("Hello"); // false, because s2 is from String Pool and s3 is from heap System.out.println("s2 == s3: " + (s2 == s3)); // true, because equals() compares content System.out.println("s2.equals(s3): " + s2.equals(s3)); } } Outputs1 == s2: true s1: Hello World s2: Hello s1 == s2: false s2 == s3: false s2.equals(s3): true Explanation:s1 and s2 initially point to the same object in the String Pool.After s1.concat(" World"), a new string object is created for "Hello World".s3 is created using new String("Hello") and stored in the heap, separate from the String Pool.== checks reference equality: s2 == s3 is false..equals() checks content equality, so s2.equals(s3) is true.Why Strings Are Designed to Be ImmutableMemory Efficiency: The String Pool allows multiple references to share the same string object safely.Thread Safety: Immutable objects are inherently safe for multi-threaded access.Hashcode Reliability: Strings are commonly used as keys in HashMap; immutability ensures the hashcode remains consistent.Performance Optimization: JVM can optimize immutable strings, including interning, which saves memory and improves speed. V vaibhav shukla 5 Follow 177 Article Tags : Java Java-Strings Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings8 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java9 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read My Profile ${profileImgHtml} My Profile Edit Profile My Courses Join Community Transactions Logout Like