1. Introduction
A String in Java represents a sequence of characters and is implemented as a reference type (a class), not a primitive. The single most important fact about String is that it is immutable — once a String object is created, its character content can never change.
Cricket analogy: A player's official career record, once entered into the ICC database after a match, is treated as a reference to a fixed record rather than a raw number you can edit—like String being a reference type whose content, once set, is immutable.
Every operation that appears to 'modify' a string, such as concatenation or replace(), actually creates and returns a brand new String object, leaving the original untouched.
Cricket analogy: When a scorer "corrects" a batsman's recorded score by replacing "45" with "45*", the original entry in the old scorecard printout isn't altered—a fresh scorecard is issued, just as replace() returns a new String and leaves the original untouched.
2. Syntax
// String literal (uses the string pool)
String a = "hello";
// Explicit object creation (bypasses the pool)
String b = new String("hello");
// Concatenation creates a new object
String c = a + " world";
// Comparing content
boolean sameContent = a.equals(b); // true
boolean sameRef = (a == b); // false3. Explanation
Java maintains a special memory region called the string constant pool (part of the heap). When you write a string literal like "hello", the JVM checks the pool first: if an identical literal already exists, the new reference points to that same pooled object instead of creating a duplicate. This is called string interning.
Cricket analogy: The ICC maintains a single official register of team names—when a new match report says "India", it points to the same existing "India" record instead of creating a duplicate entry, just like the JVM's string constant pool reusing literals via interning.
Because of pooling, "a" == "a" evaluates to true — both literals reference the exact same pooled object. However, new String("a") explicitly forces creation of a new object on the heap outside the pool, so new String("a") == "a" evaluates to false even though the content is identical.
Cricket analogy: Two match reports both referencing the pooled team name "India" point to the identical official record, so they're recognized as the same reference, but a fan's hand-written scorecard reading "India" is a separate physical copy, not the same object—like new String("a") != "a".
Critical interview trap: == compares object references (memory addresses), not content. Always use .equals() (or .equalsIgnoreCase() for case-insensitive checks) to compare string content. Using == on strings created with 'new' will silently give wrong results.
Because strings are immutable, they are inherently thread-safe and safe to use as HashMap keys — their hash code can be cached once and never becomes stale.
4. Example
public class StringImmutabilityDemo {
public static void main(String[] args) {
String s1 = "java";
String s2 = "java";
String s3 = new String("java");
System.out.println("s1 == s2 : " + (s1 == s2));
System.out.println("s1 == s3 : " + (s1 == s3));
System.out.println("s1.equals(s3) : " + s1.equals(s3));
String original = "hello";
String upper = original.toUpperCase();
System.out.println("original: " + original);
System.out.println("upper: " + upper);
}
}5. Output
s1 == s2 : true
s1 == s3 : false
s1.equals(s3) : true
original: hello
upper: HELLO6. Key Takeaways
- String objects are immutable — every 'modifying' operation returns a new String.
- String literals are interned in the string constant pool to save memory.
- "a" == "a" is true (same pooled object), but new String("a") == "a" is false (different objects).
- Always use .equals() to compare string content, never == unless intentionally checking reference identity.
- Because strings are immutable, they are thread-safe and reliable as HashMap keys.
- Concatenating strings in a loop with + repeatedly creates new objects — prefer StringBuilder for heavy concatenation.
Practice what you learned
1. What does it mean for String to be immutable in Java?
2. Given String x = "cat"; String y = "cat";, what does x == y evaluate to?
3. Given String x = new String("cat"); String y = "cat";, what does x == y evaluate to?
4. What is the correct way to compare the content of two String objects?
5. What happens in memory when you call str.toUpperCase() on a String?
Was this page helpful?
You May Also Like
StringBuilder and StringBuffer in Java
Compare Java's mutable string alternatives StringBuilder and StringBuffer, their key methods, and when to choose each for performance and thread safety.
Common String Methods in Java
A practical reference to the most frequently used Java String methods, including comparison, searching, splitting, and case-conversion operations.
Arrays in Java
Learn how Java arrays store fixed-size, homogeneous collections of elements on the heap, including declaration, default values, and common runtime errors.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics