■ JAVA STRING – MASTER COMPLETE GUIDE (FINAL)
This document is a single, continuous-flow source of truth for Java Strings. It reads like a textbook chapter with no
forced page breaks.
1. Fundamentals & Definition
In Java, a String is an Object that represents an immutable sequence of characters. Unlike primitive data types such
as int or char, String is a reference type implemented as a final class in the [Link] package. Key points: • String is
a class, not a primitive • Used to store text such as names, messages, URLs, file paths, JSON, XML • The String
class is final, so it cannot be inherited Internal Storage (Java 9+ Compact Strings): • Stored internally as byte[]
instead of char[] • Uses an encoding flag (LATIN1 or UTF16) • Reduces memory consumption significantly
2. Memory Architecture: String Constant Pool (SCP)
Java optimizes memory usage for Strings using the String Constant Pool. String Constant Pool: • A special memory
area inside Heap memory • Stores only one copy of each unique String literal • Multiple references can point to the
same pooled object String Literal Creation: String s1 = "Java"; • JVM checks SCP for an existing "Java" • If found,
reference is reused • If not found, object is created in SCP Using new Keyword: String s2 = new String("Java"); •
Always creates a new object in Heap memory • Does not reuse SCP object automatically intern() Method: • Moves a
heap String into SCP if not already present • Returns pooled reference
3. The Power of Immutability
String s1 = "Apple";
[Link](" Pie");
[Link](s1); // Apple
s1 = [Link](" Pie");
[Link](s1); // Apple Pie
Once a String object is created, its value cannot be changed. Any modification creates a new object in memory.
Why Strings are immutable: • Security (prevents tampering of sensitive data) • HashMap safety (hashCode caching)
• Thread safety (safe sharing) • Memory efficiency via pooling Performance note: • Avoid String concatenation in
loops • Use StringBuilder or StringBuffer instead
4. String Methods (Complete Reference)
4.1 Basic Methods (Inspection)
"Java".length(); // 4
"".isEmpty(); // true
" ".isBlank(); // true
"Sky".charAt(1); // 'k'
"A".hashCode(); // 65
4.2 Comparison Methods
String s1 = "Java";
String s2 = "java";
[Link](s2); // false
[Link](s2); // true
"Apple".compareTo("Banana"); // negative value
4.3 Search & Localization Methods
String text = "Java is fun, Java is powerful";
[Link]("Java"); // 0
[Link]("Java"); // 13
[Link]("fun"); // true
4.4 Transformation Methods
String raw = " Java ";
[Link](); // "Java"
[Link](); // "Java"
[Link](0,2); // "Ja"
[Link](); // "JAVA"
"Hi".repeat(3); // "HiHiHi"
4.5 Decomposition & Joining Methods
"Red,Green,Blue".split(",");
[Link]("-", "2026","01","04");
[Link]("Score: %d/%d", 9, 10);
5. Mutable Alternatives
StringBuilder sb = new StringBuilder("Start");
[Link](" Middle");
[Link](5, "!");
[Link]();
[Link](0,5);
String result = [Link]();
StringBuffer is synchronized and thread-safe. Use it only when multiple threads access the same object.
6. String vs StringBuilder vs StringBuffer
String -> Immutable, Thread-safe, Slow
StringBuilder -> Mutable, Not thread-safe, Fastest
StringBuffer -> Mutable, Thread-safe, Moderate
7. Advanced & Modern Concepts
Text Blocks (Java 15+)
String json = """
{
"id": 1,
"name": "Gemini"
}
""";
String Templates (Java 21+)
String user = "Admin";
String msg = STR."User: \{user} logged in";
Security:
Use char[] for passwords, not String.