How to Prepare for Java Interviews

Java interviews typically test three areas: core language knowledge, object-oriented design, and practical problem-solving. This guide focuses on the conceptual questions that interviewers return to most often — along with the kind of clear, confident answers that signal genuine understanding.

Core Java Questions

1. What is the difference between == and .equals()?

== compares references — it checks if two variables point to the same object in memory. .equals() compares content — it checks if two objects are logically equal.

String a = new String("hello");
String b = new String("hello");
System.out.println(a == b);       // false (different objects)
System.out.println(a.equals(b));  // true  (same content)

2. What is the difference between final, finally, and finalize?

  • final — A keyword to declare constants, prevent method overriding, or prevent class inheritance.
  • finally — A block in try/catch that always executes, used for cleanup.
  • finalize — A deprecated method called by the GC before an object is collected. Avoid using it.

3. What is autoboxing and unboxing?

Autoboxing is the automatic conversion of a primitive type (e.g., int) to its wrapper class (Integer). Unboxing is the reverse. Java performs this automatically but be aware of null pointer exceptions when unboxing a null wrapper.

4. What is the Java Memory Model and what are heap and stack?

  • Stack — Stores method call frames, local variables, and references. Each thread has its own stack.
  • Heap — Stores all objects created with new. Shared across threads and managed by the garbage collector.

OOP Questions

5. What are the four pillars of OOP?

  1. Encapsulation — Bundling data and methods, hiding internal state with access modifiers.
  2. Inheritance — A class can extend another, reusing and specializing behavior.
  3. Polymorphism — One interface, many implementations (method overriding and overloading).
  4. Abstraction — Hiding complex implementation behind simple interfaces or abstract classes.

6. What is the difference between an abstract class and an interface?

FeatureAbstract ClassInterface
InstantiationNoNo
Multiple inheritanceNo (single)Yes (multiple)
Can have state (fields)YesOnly constants
ConstructorYesNo
Default methodsYesYes (Java 8+)

Frequently Tested Topics

7. How does HashMap work internally?

A HashMap uses an array of "buckets." The key's hashCode() determines which bucket an entry goes into. If multiple keys hash to the same bucket (a collision), they are stored in a linked list (or a balanced tree in Java 8+ when the list grows long). The equals() method is then used to find the exact key.

8. What is the difference between Comparable and Comparator?

  • Comparable — Defines a class's natural ordering via compareTo(). The class itself implements it.
  • Comparator — An external strategy for ordering, passed where needed. Useful when you can't modify the class or need multiple sort orders.

Interview Tips

  • Think out loud — Interviewers want to see your reasoning process, not just the answer.
  • Ask clarifying questions — It shows thoughtfulness and prevents wasted effort.
  • Know your fundamentals cold — String handling, Collections, and OOP principles come up in nearly every interview.
  • Practice on a whiteboard or paper — Many interviews still use this format; practicing helps you stay calm.