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 == and equals() in Java, and what is the String pool?
== checks whether two references point to the same object, while equals() checks whether two objects have the same content (if the class overrides it, as String does). String literals are stored once in the String pool, which is why two identical literals can be == but new String("x") cannot.
BeginnerIn-depth3 min read - What is the difference between checked and unchecked exceptions in Java?
Checked exceptions (subclasses of Exception other than RuntimeException) must be caught or declared with throws, and the compiler enforces this. Unchecked exceptions (RuntimeException and its subclasses, plus Errors) are not checked at compile time and usually signal programming bugs.
BeginnerIn-depth3 min read - What is the difference between final, finally and finalize in Java?
final is a keyword that stops change (a variable cannot be reassigned, a method cannot be overridden, a class cannot be extended). finally is a block that runs after try/catch, usually for cleanup. finalize() was a method the garbage collector might call before removing an object; it is deprecated for removal and should not be used.
BeginnerIn-depth3 min read - What is the difference between JDK, JRE and JVM?
The JVM runs Java bytecode. The JRE is the JVM plus the standard libraries needed to run a program. The JDK is the JRE plus developer tools such as the javac compiler, so you need the JDK to write and build Java, and only a runtime to run it.
BeginnerIn-depth3 min read - 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 - What does the static keyword mean in Java?
static means a member belongs to the class itself, not to any one object. A static variable has one shared copy, a static method can be called without creating an object, a static block runs once when the class is loaded, and a static nested class does not need an outer object.
IntermediateIn-depth3 min read - What is the difference between ArrayList and LinkedList in Java?
ArrayList is backed by a resizable array, so reading by index is O(1) but inserting or removing in the middle shifts elements. LinkedList is a doubly linked list, so adding or removing at the ends is O(1) but reaching an index means walking the nodes. In practice ArrayList is the better default almost every time.
IntermediateIn-depth3 min read - How does HashMap work internally in Java?
HashMap stores entries in an array of buckets. It hashes the key, picks a bucket from that hash, and stores the entry there; keys that land in the same bucket form a linked list, which since Java 8 turns into a red-black tree when it gets long. When the map is 75% full it doubles the array and redistributes entries.
AdvancedIn-depth4 min read