Concept
Strings
Every "change" creates a new String object. 💡 Why immutable? Thread-safe, safe as HashMap keys, string pool caching.
Also known as String.
Where it fits
Packages: Performance (pkg19performance), Core Java (pkg1core)
Learn
Existing chapters for this concept.
- Strings
Every "change" creates a new String object. 💡 Why immutable? Thread-safe, safe as HashMap keys, string pool caching.
Open lesson →
See it in code
Existing Java examples.
- core 9 Strings Demo
- Strings are immutable: every "modification" creates a new String. - Literals are interned in the string pool; `new String("x")` is a new object. - Use…
Open example →
Practice
Existing LeetCode material in JavaForge.
- Longest Common Subsequence
2D DP on character prefixes of both strings. Time O(mn), Space O(mn)
Open practice → - Valid Palindrome
Two pointers skip non-alphanumeric, compare lowercased. Time O(n), Space O(1)
Open practice → - Valid Parentheses
Stack matches closing bracket to top opening. Time O(n), Space O(n)
Open practice → - Encode and Decode Strings
Length-prefix encoding: len#payload for each string. Time O(total chars), Space O(total chars)
Open practice → - Longest Substring Without Repeating Characters
Sliding window with last-seen index map. Time O(n), Space O(min(n, alphabet))
Open practice → - Longest Repeating Character Replacement
Sliding window; shrink when window - maxFreq > k. Time O(n), Space O(26)
Open practice → - Group Anagrams
HashMap keyed by sorted char signature. Time O(n k log k), Space O(nk)
Open practice → - Longest Palindromic Substring
Expand around center for odd/even lengths. Time O(n^2), Space O(1)
Open practice → - Palindromic Substrings
Expand around each center counting palindromes. Time O(n^2), Space O(1)
Open practice → - Minimum Window Substring
Expand right until valid; shrink left while valid. Time O(n), Space O(1) alphabet
Open practice → - Longest Common Prefix
Compare characters column-wise across all strings. Time O(n*m), Space O(1)
Open practice → - Isomorphic Strings
Two hash maps enforce one-to-one char mapping. Time O(n), Space O(1)
Open practice → - Find All Anagrams in a String
Sliding window with char frequency match count. Time O(n), Space O(1)
Open practice →
Prepare
Existing interview questions.
- Why are Strings immutable?
Safety, caching (string pool), thread-safety, and hashcode caching.
Open question → - `String` vs `StringBuilder` vs `StringBuffer`?
String immutable; StringBuilder mutable (not thread-safe, fast); StringBuffer mutable (synchronized).
Open question → - What is the string pool?
Clear naming, small methods, immutability, proper exceptions, tests, and idiomatic APIs.
Open question → - synchronized on String/Integer?
Prefer immutability and high-level concurrency utilities over low-level locks.
Open question → - Strings & Performance — Interview Questions (82+)
NPE if reference null on instance concat.
Open question → - Why are Strings immutable in Java?
Security, thread-safety, string pool, and cached hashCode.
Open question → - StringBuilder capacity tuning?
Set initial capacity if final size known to avoid resize copies.
Open question → - `String.format` vs `MessageFormat` vs text blocks?
for printf-style; for locale patterns; text blocks for multi-line literals.
Open question → - `String` vs `char[]` for passwords?
can be zeroed after use; stays in pool/heap until GC.
Open question → - Compact Strings (Java 9+)?
Internal + coder (LATIN1 or UTF16) saves memory for ASCII-heavy strings.
Open question → - When is `StringBuilder` slower than `+`?
Few fixed concatenations — compiler may use invokedynamic/string concat factory (Java 9+).
Open question →
Java versions
- Strings in switch
Strings in switch. A switch can select on a string. The example uses the Java 7 form: colon labels and break, not arrow labels.
Open version note → - String and file helpers
String and file helpers. String gains isBlank, strip, lines, and repeat. Files gains readString and writeString. These are library methods, not a separate…
Open version note → - String Templates (Preview)
String Templates (Preview). String templates are a preview for interpolating values into a string or another result. They are not a permanent feature.
Open version note → - String Templates (Second Preview)
String Templates (Second Preview). String templates are previewed a second time. They are not finalized in this release.
Open version note →