Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 7 of 10

String & String Deep Dive

In Java, String is an immutable class - once created, it can never be changed. Every String operation (concatenation, replacement) creates a NEW String object. Understanding String immutability, the String pool, and StringBuilder is critical for writing efficient code.

15 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

String Immutability & Pool

  • String is immutable - methods like toUpperCase() return a NEW String, never modify the original.
  • String pool - JVM caches string literals. "hello" == "hello" is true (same pool object).
  • new String("hello") creates a heap object outside the pool - avoid this form.
  • Always compare String values with .equals(), never with ==.
  • String is thread-safe because of immutability - safe to share between threads.

String Methods & StringBuilder

String Methods & StringBuilder
public class StringDeepDive {
    public static void main(String[] args) {
        String s = "  Hello, Java World!  ";

        // Common String methods
        System.out.println(s.trim());                     // "Hello, Java World!"
        System.out.println(s.strip());                    // Java 11+, Unicode-aware trim
        System.out.println(s.toLowerCase());              // "  hello, java world!  "
        System.out.println(s.toUpperCase());              // "  HELLO, JAVA WORLD!  "
        System.out.println(s.length());                   // 22
        System.out.println(s.trim().contains("Java"));    // true
        System.out.println(s.trim().startsWith("Hello")); // true
        System.out.println(s.trim().indexOf("Java"));     // 7
        System.out.println(s.trim().replace("Java", "Python")); // Hello, Python World!
        System.out.println(s.trim().substring(7, 11));    // "Java"

        // String.format (preferred for complex building)
        String name = "Alice";
        int age = 30;
        String profile = String.format("Name: %s, Age: %d", name, age);
        System.out.println(profile);  // Name: Alice, Age: 30

        // split and join
        String csv = "Java,Python,Go,Rust";
        String[] langs = csv.split(",");        // ["Java", "Python", "Go", "Rust"]
        String joined = String.join(" | ", langs); // "Java | Python | Go | Rust"
        System.out.println(joined);

        // StringBuilder - for many concatenations in loops (fast!)
        // BAD: String result = ""; for (...) result += item; // creates N new Strings!
        // GOOD:
        StringBuilder sb = new StringBuilder();
        for (int i = 1; i <= 5; i++) {
            sb.append("Item ").append(i).append(", ");
        }
        sb.setLength(sb.length() - 2);   // remove trailing ", "
        System.out.println(sb.toString()); // "Item 1, Item 2, Item 3, Item 4, Item 5"

        // Text blocks (Java 15+) - multiline strings
        String json = """
                {
                    "name": "Alice",
                    "age": 30
                }
                """;
        System.out.println(json);
    }
}

Quick Quiz

Tip

Tip

Practice String String Deep Dive in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for String & String Deep Dive

Practice Task

Note

Practice Task - (1) Write a working example of String String Deep Dive from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with String String Deep Dive is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • In Java, String is an immutable class - once created, it can never be changed.
  • String is immutable - methods like toUpperCase() return a NEW String, never modify the original.
  • String pool - JVM caches string literals. "hello" == "hello" is true (same pool object).
  • new String("hello") creates a heap object outside the pool - avoid this form.

Finished this lesson?

0 of 10 in Module 1
Sign in to save progress

Topics in This Module

0/10 done