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
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
