Strings in Java
Deep dive into String immutability, StringBuilder for mutable strings, string methods, and modern Text Blocks (Java 13+) for multiline strings.
String Basics
A `String` in Java is an object that represents a sequence of characters. Strings are **immutable** — once created, their value can never be changed. Any operation that appears to modify a string actually creates a new one.
1. Creating Strings
Strings can be created with a literal (recommended) or with `new String()`. Literals are stored in the String Pool for memory efficiency.
2. String Immutability
Every "modification" of a String creates a brand new String object. The original is never changed.
3. String Comparison
Use `.equals()` for value equality, `.equalsIgnoreCase()` for case-insensitive comparison, and `.compareTo()` for lexicographic ordering.
Common String Methods
The `String` class has over 60 methods. These are the ones you will use in almost every Java project.
1. Searching & Checking
Methods to find characters or substrings and check the contents of a string.
2. Transforming Strings
Methods to change case, trim whitespace, replace content, and extract substrings.
3. split() and join()
Break a string into parts, or join multiple strings into one.
StringBuilder (Mutable Strings)
`StringBuilder` is a mutable sequence of characters. Unlike `String`, it modifies its internal buffer in-place without creating new objects on each change. Use it whenever you build a string incrementally — especially inside loops.
1. StringBuilder Basics
Append, insert, delete, and reverse characters without creating new String objects each time.
2. StringBuilder in a Loop
The classic use case: building a string from many pieces without the overhead of `+` in a loop.
3. Method Chaining
StringBuilder methods return `this`, so you can chain multiple calls in one expression.
String Formatting & Interpolation
Java does not have native string interpolation like some languages, but `String.format()`, `formatted()` (Java 15+), and `printf()` provide powerful formatting. Text Blocks (Java 15+) solve multiline string boilerplate.
1. String.format() and formatted()
Use format specifiers to embed values cleanly into a string template.
2. Text Blocks (Java 15+)
Write multiline strings cleanly without escape sequences — perfect for JSON, HTML, SQL, and other structured text.
3. Converting Other Types to String
Several ways to turn numbers, booleans, and objects into their String representation.
Indexing, Length & Traversal
Strings in Java are zero-indexed sequences of `char` values. You can access any character by its index, get the total count with `length()`, and traverse the characters with a classic `for` loop or a range-based loop using `toCharArray()`.
1. Accessing a Character at a Specific Index
Use `charAt(index)` to get the character at a position. Indices start at 0.
2. Getting String Length
Use `length()` to get the number of characters in a string.
3. Traversing a String
Loop through every character using a classic `for` loop with index, or a for-each loop via `toCharArray()`.
String Transformations
Java does not have a built-in `reverse()` method on `String` (because strings are immutable), but `StringBuilder` provides one. Case conversion and repeat are built into `String` itself.
1. Reversing a String
Three common approaches: StringBuilder.reverse(), manual loop, and char array swap.
2. Uppercase, Lowercase & Repeat
Built-in methods for changing case and repeating strings.
3. Concatenating Strings
Three ways to concatenate: `+` operator, `concat()` method, and `StringBuilder`.
ASCII Conversions
In Java, `char` is a numeric type backed by a 16-bit Unicode code point. You can convert freely between `char` and `int` using casting, which makes ASCII arithmetic straightforward.
1. Character to ASCII Code
Cast a `char` to `int` to get its Unicode/ASCII code point value.
2. ASCII Code to Character
Cast an `int` back to `char` to get the character for a given code point.
Searching & Replacing
Java's `String` class has two families of replace methods: plain-text replacements (`replace()`) and regex-based replacements (`replaceAll()`, `replaceFirst()`). Regex gives you powerful pattern-matching for complex search-and-replace operations.
1. replace(), replaceAll(), replaceFirst()
`replace()` works on literal strings/chars. `replaceAll()` and `replaceFirst()` accept regex patterns.
2. Trimming (Left, Right, Both)
Remove leading and trailing whitespace from strings.