100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
Java

Strings in Java

Understand Java's String class, its immutability, the string constant pool, and why == should never be used to compare string content.

Arrays & StringsBeginner10 min readJul 7, 2026
Analogies

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

java
// 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);        // false

3. 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

java
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

text
s1 == s2 : true
s1 == s3 : false
s1.equals(s3) : true
original: hello
upper: HELLO

6. 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

Was this page helpful?

Topics covered

#Java#JavaProgrammingStudyNotes#Programming#StringsInJava#Strings#Syntax#Explanation#Example#StudyNotes#SkillVeris