java.lang · java.base · since Java 1.0
String
public final class String
implements CharSequence, Comparable<String>, SerializableImmutable UTF-16 character sequence. The most used class in Java: literals are pooled, methods return new strings, and comparison is always equals(), never ==.
- Immutable — safe to share, cache, and key maps with
- length()/charAt() count UTF-16 code units, not visible characters
- Compact strings: Latin-1 content stored as bytes internally
Key methods
boolean equals(Object) / equalsIgnoreCase(String) | Content comparison — the correct way. |
String substring(int begin, int end) | Half-open range copy. |
boolean contains/startsWith/endsWith | Containment tests. |
String strip() / isBlank() | Unicode-aware trim and whitespace test. |
String replace(CharSequence, CharSequence) | Literal replacement of all occurrences. |
String[] split(String regex) | Regex split — escape literal dots! |
static String join(CharSequence delim, ...) | Join pieces with a delimiter. |
String formatted(Object... args) | printf-style formatting (instance form of String.format). |
IntStream chars() / codePoints() | Stream over code units / code points. |
String intern() | Canonical pooled instance — rarely needed, measure before using. |
Example
String csv = String.join(", ", List.of("a", "b", "c"));
String padded = "%5d".formatted(42);
"Ciao 🇮🇹".length(); // 9 — code units, not characters!