Nearby lessons
109 of 125Java - Strings
- Why String objects are immutable
- String vs StringBuffer vs StringBuilder
- The most important methods of String
- == vs equals() — the classic interview question
- How to split, join and manipulate text
Strings is a core concept of the Java language. This lesson explains What is a String?, Strings Are Immutable and String Pool — Two Statements, One Big Difference with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.
What is a String?
A String in Java is a sequence of characters. But it is a special thing: String is not a primitive type, it is a class that Java provides for us. Every time you write double quotes, Java creates a String object.
Strings are stored in a special area of memory called the String Constant Pool. When you write a string literal, Java first checks the pool — if the same value already exists, it reuses that object instead of creating a new one.
Strings Are Immutable
Immutable means: once a String object is created, its content can never be changed. If you try to change it, Java creates a new String object and leaves the old one as it is.
Why make Strings immutable? Main reasons: security (strings like passwords and class names must not be changed by accident), thread safety (many threads can safely share one immutable object), and caching (reusing the same value from the pool saves memory).
String Pool — Two Statements, One Big Difference
The classic material asks: what is the difference between these two statements?
- Statement 1 creates (or reuses) the String in the String Constant Pool (inside the method area).
- Statement 2 creates a new object in the heap (plus, the literal "abc" may also go to the pool).
Important note about memory: objects in the String Constant Pool are not eligible for garbage collection; objects in the heap are eligible. And remember the golden rule — s1 == s2 is false (different objects), s1.equals(s2) is true (same content).
== vs equals() — The Classic Question
This is the most asked String question in interviews. Understand it perfectly:
- == compares references — are they the same object in memory?
- .equals() compares content — is the text inside the same?
toString() — Print an Object Nicely
When you print an object with System.out.println(obj), Java calls its toString() method. The default toString() prints a code like Student@4aa298b7 (class name + memory code). We usually override it to print useful information.
String Constructors
A String object can be created in many ways. The important constructors:
- String is a class, not a primitive; it is immutable.
- Immutability gives security, thread safety and pool caching.
- Use StringBuilder (fastest, not thread-safe) or StringBuffer (thread-safe) when text changes often.
- == compares references; .equals() compares content. Always use .equals() for Strings.
- Override toString() to print objects meaningfully.
- Text blocks (Java 15+) make multi-line strings clean.