Java Topics
Immutable Strings
Last Updated : 26 May, 2026
title: Immutable Strings in Java
title: Immutable Strings in Java description: String immutability kya hai aur kyun hai
Immutability kya hai?
String object create hone ke baad uski value change nahi ho sakti. Modification pe naya object banta hai.
String s = "Hello";
s.toUpperCase(); // Naya object bana, s unchanged
System.out.println(s); // "Hello"
String upper = s.toUpperCase(); // Nayi reference rakho
System.out.println(upper); // "HELLO"Kyun Immutable?
1. String Pool possible hoti hai
String s1 = "Java";
String s2 = "Java"; // Same pool object reuse
// Agar mutable hota aur s1 change karta, s2 bhi affect hota2. Thread Safety
Multiple threads safely same String read kar sakti hain bina synchronization ke.
3. Security
File paths, network URLs, passwords — change nahi ho sakte.
4. HashCode Caching
HashMap mein key ki tarah use hoti hai. HashCode cache ho sakta hai.
Memory Impact
String s = "Hello";
s = s + " World";
// Memory mein:
// "Hello" → String Pool (reachable nahi, GC karega)
// "Hello World" → naya object, s point karta haiString Interning
String s1 = new String("Java");
String s2 = s1.intern(); // Pool mein daalo
String s3 = "Java";
System.out.println(s2 == s3); // trueExam Focus
Revise definitions, diagrams, examples, and short-answer points for Immutable Strings.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Java topic.
Search Terms
java, java programming, core java, java master course, java notes, master, course, fundamentals
Related Java Topics