Java / Java 21 Interview Questions
What important String methods were added from Java 11 through Java 21?
The String class received significant API improvements across several Java releases. Knowing the version they arrived in is common interview territory.
| Method | Since | Purpose |
|---|---|---|
| isBlank() | 11 | Returns true if string is empty or contains only whitespace |
| strip() / stripLeading() / stripTrailing() | 11 | Unicode-aware whitespace stripping (vs trim() which uses ASCII <= 32) |
| lines() | 11 | Returns a Stream<String> of lines split by line terminators |
| repeat(int n) | 11 | Returns the string repeated n times |
| indent(int n) | 12 | Adds/removes leading whitespace per line |
| formatted(Object... args) | 15 | Instance version of String.format() |
| stripIndent() | 15 | Removes incidental whitespace (used by text blocks) |
| translateEscapes() | 15 | Interprets \n \t etc. as escape sequences |
| chars() / codePoints() | 9 | Returns IntStream of char/codepoint values |
// isBlank and strip — Java 11
" \t ".isBlank(); // true
" hello ".strip(); // "hello" (Unicode-aware)
" hello ".trim(); // "hello" (ASCII only — legacy)
// lines() — Java 11
"a\nb\nc".lines().count(); // 3 (Stream)
// repeat() — Java 11
"ab".repeat(3); // "ababab"
// formatted() — Java 15
"Hello %s, you are %d".formatted("Alice", 30);
// "Hello Alice, you are 30"
// indent() — Java 12
"hello\nworld".indent(4);
// " hello\n world\n"
// chars() — Java 9
"Java".chars().forEach(c -> System.out.print((char) c + " "));
// J a v a
More Related questions...