Interview Q&A / Java
Java interview questions
Collections, the JVM, strings, exceptions and threads, the backbone of most campus placement rounds.
Prefer rapid revision? Practise Java short interview answers →
- What is the difference between String, StringBuilder and StringBuffer?
String is immutable, so every change creates a new object. StringBuilder and StringBuffer are mutable and change the same object in place. StringBuffer's methods are synchronized (thread-safe but slower), while StringBuilder is not synchronized and is the usual choice for building strings in a single thread.
BeginnerIn-depth3 min read - Why is the String class immutable in Java?
Once created, a String cannot change. This makes the string pool safe to share, lets the hash code be cached for fast HashMap use, keeps strings thread-safe, and stops callers from altering values used for security, such as file paths or URLs.
BeginnerQuick answer1 min read