java.lang.String Catechism
summary
Machine benchmarks with JMH show naive concatenation can incur ~30x slowdown versus StringBuilder because the compiler/JIT rewrites concatenation but bails out on side effects; logging benefits from Supplier lambdas but explicit guards remain fastest. Strings store a final char[] and cached hash (zero is the “not computed” sentinel), so substring retains big char[] causing OOM in older JDKs (JDK8 copies), intern() hits a fixed VM string table and is far slower than a custom interner, and HashMap collisions enable DoS mitigated by Java 8 treeification. Beware regex catastrophic backtracking; Boyer‑Moore beats indexOf on huge texts.
AI-written summary · about
chapters (19)
- 0:00Introduction and disclaimer
- 1:14Why strings matter
- 3:42String internals
- 6:29String concatenation costs
- 9:21StringBuilder optimization
- 11:30Integer to string vs concat
- 13:14Side effects in concatenation
- 15:00Lazy logging with lambdas
- 18:10String hash code internals
- 20:19HashMap performance pitfalls
- 26:16Substring memory issues
- 29:12Deduplication vs interning
- 33:47String deduplication in JVM
- 36:37String comparison optimizations
- 40:28String split performance
- 42:40Regex catastrophic backtracking
- 45:07CharAt vs char array access
- 48:19String search algorithms
- 52:15Conclusion and Q&A
