In this article
  1. Core classes and the language
  2. Collections and streams
  3. Memory and the JVM
  4. Concurrency
  5. Frameworks and tooling
  6. Follow-up questions
  7. Sources

Core classes and the language

These questions assume you can already write short programs; the coding interview guide covers the problem-solving rounds that surround them.

Why are Java strings immutable, and what is the difference between == and equals?

A String cannot be changed after it is created, which lets the runtime share one object between many references (Java Standard Edition (SE) 21 String Javadoc, October 2026). The == operator compares references and equals compares text. In the run, new String("java") == "java" was false, equals was true, and intern() returned the shared literal. Compare strings with equals.

Why it is asked: it separates object identity from value equality. Read the full explanation: String Javadoc.

What is the difference between String and StringBuilder?

A String is immutable, so every change makes a new object, while a StringBuilder is a mutable sequence of characters that you change in place (StringBuilder Javadoc, October 2026). In the run, "ab".concat("c") left the original at ab, and append returned the same builder, now abc. The Javadoc says a StringBuilder is not a synchronized class, so share one across threads only with your own locking.

Why it is asked: it checks whether you know what repeated concatenation in a loop costs. Read the full explanation: StringBuilder Javadoc.

Why is Integer 127 == 127 true but 128 == 128 false?

The Integer.valueOf method always caches the values from -128 to 127 and may cache others, so boxing 127 twice gives one object, while boxing 128 twice gave two (Java SE 21 Integer Javadoc, October 2026)). In the run, == was true for 127 and false for 128, and equals was true for 128. Compare boxed numbers with equals.

Why it is asked: it tests whether you know that boxed values are objects. Read the full explanation: Integer.valueOf Javadoc).

What is the contract between equals and hashCode?

If two objects are equal according to equals, calling hashCode on each must give the same integer; unequal objects may share a hash code (Java SE 21 Object Javadoc, October 2026). Break it and hash-based collections misbehave: in the run, two equal objects without a matching hashCode gave a HashSet of size 2, and with one it gave size 1. Override both together.

Why it is asked: it is the standard source of silent collection bugs. Read the full explanation: hash-based sets and maps, or the Object Javadoc.

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

An abstract class is a class that is incomplete: you cannot create an instance of it directly, and a subclass has to complete it (Java Language Specification (JLS) section 8.1.1.1, SE 21, October 2026). An interface can carry default methods, which give a body to any implementing class that does not override them (JLS section 9.4); in the run a class with no code of its own returned default hello. A class extends one class but may implement many interfaces.

Why it is asked: it shows whether you choose a type by what it must promise or by what it must share. Read the full explanation: JLS section 9.4.

What is the difference between method overloading and overriding?

Overloading gives several methods one name and different parameters, and the compiler picks one from the types of the arguments (JLS section 15.12.2, SE 21, October 2026). Overriding replaces an inherited method, and the code that runs depends on the class of the object at run time (JLS section 15.12.4). In the run, a String held in an Object variable chose the Object overload, and an Animal variable holding a Dog printed Woof.

Why it is asked: it separates compile-time choice from run-time choice. Read the full explanation: JLS section 15.12.4.

What is the difference between checked and unchecked exceptions?

Unchecked exceptions are RuntimeException, Error and their subclasses; every other Throwable is checked (Java Language Specification (JLS) section 11.1.1, SE 21, October 2026). The compiler requires a program to have handlers for the checked exceptions a method or constructor can throw (JLS section 11.2): catch it or declare it. IOException is checked, NullPointerException is unchecked. Errors are exceptions from which ordinary programs are not ordinarily expected to recover.

Why it is asked: it shows whether you design error handling or only catch everything. Read the full explanation: JLS section 11.2.

What does try-with-resources do?

A try-with-resources statement declares resources, which are objects that implement AutoCloseable, and closes each one when the block ends, normally or by exception (The Java Tutorials, October 2026). Resources close in the opposite order of creation; the run printed body, closed B, closed A. If the block and a close both throw, the close exception is suppressed: in the run the body exception carried one suppressed exception.

Why it is asked: leaked files and connections are a common production fault. Read the full explanation: The Java Tutorials, try-with-resources.

What is a Java record?

A record is a class for plain data: its header lists the components, and the compiler generates the fields, accessors, constructor, equals, hashCode and toString (JDK Enhancement Proposal (JEP) 395, final in Java 16, updated September 2026). Records cannot extend another class. In the run, new Point(1, 2) printed Point[x=1, y=2], two records with the same components were equal, and reflection reported the class as final.

Why it is asked: it checks whether you know modern Java, not only Java 8. Read the full explanation: JEP 395.

Collections and streams

What is the difference between ArrayList and LinkedList?

An ArrayList is a resizable array whose get and set run in constant time and whose add runs in amortized constant time (ArrayList Javadoc, October 2026). A LinkedList is doubly linked, and an operation that indexes into it traverses from the nearer end (LinkedList Javadoc, October 2026). Default to ArrayList. These complexities are the documented ones; this page did not time them.

Why it is asked: it tests whether you reason from the data structure, not from a slogan. Read the full explanation: ArrayList Javadoc.

Why does removing from a list inside a for-each loop throw ConcurrentModificationException?

The list iterators are fail-fast: if the list changes structurally after the iterator is created, other than through the iterator itself, the next step usually throws ConcurrentModificationException, but only on a best-effort basis (ArrayList Javadoc, October 2026). The run threw it when 2 was removed from [1, 2, 3, 4], yet removing 2 from [1, 2, 3] threw nothing and silently skipped the last element. Use removeIf, which gave [1, 3, 4], or the iterator's own remove.

Why it is asked: it is a classic bug with a one-line fix. Read the full explanation: ArrayList Javadoc.

How does a HashMap work internally?

A HashMap is a hash table: the capacity is the number of buckets, the default is 16 buckets with a load factor of 0.75, and when the entries exceed capacity times load factor the table is rebuilt with about twice as many buckets (HashMap Javadoc, October 2026). Since Java 8, a crowded bucket can become a balanced tree, making worst-case lookups logarithmic instead of linear (JEP 180, updated June 2017). In the run, 1,000 keys sharing one hash code all stayed retrievable.

Why it is asked: it joins hashing, resizing and the equals contract in one question. Read the full explanation: hash-based sets and maps, or the HashMap Javadoc.

What is the difference between HashMap and ConcurrentHashMap?

A ConcurrentHashMap is thread-safe: retrieval operations generally do not block and may overlap updates, and unlike HashMap it rejects null keys and values (ConcurrentHashMap Javadoc, October 2026). In the run a null key worked in a HashMap and threw NullPointerException in a ConcurrentHashMap. Its iterators do not throw ConcurrentModificationException, but they are meant for one thread at a time.

Why it is asked: it probes real thread-safety, not the word. Read the full explanation: ConcurrentHashMap Javadoc.

What are lambdas, functional interfaces and Optional?

A functional interface has a single abstract method, and a lambda expression or method reference supplies it (java.util.function package summary, October 2026). In the run, a Function<Integer, Integer> written as x -> x * x applied to 7 gave 49. An Optional is a container that may or may not hold a non-null value (Optional Javadoc, October 2026); an empty one mapped and given a default gave -1 without a null check.

Why it is asked: Java 8 style is the baseline of most codebases. Read the full explanation: java.util.function package summary.

Are Java streams lazy?

Intermediate operations such as filter and map are always lazy: they build a new stream, and traversal of the source does not begin until a terminal operation runs (java.util.stream package summary, October 2026). In the run a map had executed 0 times before the terminal collect and 3 times after. A short-circuiting terminal such as findFirst visited 2 of 4 elements.

Why it is asked: it tests whether you can predict when side effects happen. Read the full explanation: stream package summary.

Knowing it is half the job. Saying it is the other half.

Interview Clozo is an AI interview copilot for real interview rounds: it hears the question and puts a strong answer on your screen, and you say it in your own words. See the features or how it works.

Start Free Trial →

Memory and the JVM

What is the difference between the stack and the heap?

Each thread has a private Java Virtual Machine (JVM) stack that holds frames, meaning local variables and partial results, while the heap is shared by all threads and holds all class instances and arrays (JVM specification, chapter 2, SE 21, October 2026). A local variable that refers to an object lives on the stack; the object lives on the heap.

Each thread has a private stack and all threads share one heapBoxes: two threads each own a stack of method frames that holds local variables, while objects and arrays live on one shared heap that a garbage collector reclaims.Thread 1 stackframes, localsThread 2 stackframes, localsShared heapobjects, arraysGarbage collectorfrees unreachableDiagram: Interview Clozo, CC BY 4.0 ·interview.clozo.ai/blog/java-related-interview-questions.html
Local variables sit on a private stack; objects sit on the shared heap that the garbage collector cleans.CC BY 4.0 — source: Java related interview questions from strings and the JVM to Spring and Maven
Text description of the diagram
  • Each thread has a private stack of frames that holds local variables.
  • The heap is shared by all threads and holds every object and array.
  • A garbage collector reclaims heap storage; objects are never freed explicitly.

Why it is asked: it underlies memory errors and thread safety. Read the full explanation: JVM specification, run-time data areas.

How does garbage collection work, and which collector is the default?

Heap storage is reclaimed by an automatic garbage collector, and objects are never freed explicitly (JVM specification, October 2026). The tuning guide calls the Garbage-First (G1) collector the default; it splits the heap into equal regions and aims to meet pause-time goals with high probability, though the guide says it is not a real-time collector (Java garbage collection tuning guide, October 2026).

Why it is asked: it separates memorised terms from knowing what a pause is. Read the full explanation: G1 tuning guide.

Concurrency

What is the difference between synchronized and volatile?

A synchronized block gives mutual exclusion and also visibility: releasing a monitor happens-before every later acquisition of that same monitor (JLS section 17.4.5, SE 21, October 2026). A write to a volatile field happens-before every later read of it, so it gives visibility and ordering, but not atomicity (JLS section 8.3.1.4). In the run, four threads adding 100,000 each under synchronized ended at exactly 400,000, while the same count on a volatile field lost updates.

Why it is asked: the first answer is mutual exclusion, and the follow-up is visibility. Read the full explanation: concurrency interview questions, or JLS section 17.4.5.

What are virtual threads?

A virtual thread is an instance of java.lang.Thread that is not tied to a particular operating-system thread, and it became a final feature in JDK 21 (JEP 444, updated October 2025). When its code calls blocking input or output, the runtime suspends the virtual thread. The run started 10,000 virtual threads that each slept, and all 10,000 completed.

Why it is asked: it checks whether you know what changed after Java 17. Read the full explanation: JEP 444.

Frameworks and tooling

What is dependency injection in Spring?

Dependency injection is a form of inversion of control (IoC) where an object declares its dependencies through constructor arguments, factory-method arguments or properties, and the container supplies them when it creates the bean (Spring Framework reference, October 2026). A bean is an object that the Spring container instantiates, assembles and manages. The object no longer constructs or looks up its own collaborators.

Why it is asked: it is the base of most Spring questions. Read the full explanation: Spring IoC container introduction.

What does Spring Boot auto-configuration do?

Auto-configuration configures a Spring application from the jars on the classpath: with an embedded database on the classpath and no connection beans of your own, Boot configures an in-memory database (Spring Boot reference, October 2026). It is non-invasive, so defining your own DataSource makes it back away, and the --debug switch logs a conditions report.

Why it is asked: it separates using Boot from understanding it. Read the full explanation: Spring Boot interview questions, or the Spring Boot reference.

What is a Maven build lifecycle?

Maven defines a build lifecycle with clearly ordered phases; the default lifecycle runs validate, compile, test, package, verify, install and deploy, and running one phase runs every phase before it (Apache Maven, lifecycle guide, last published October 2026). So mvn verify compiles, tests and packages first. The other two built-in lifecycles are clean and site.

Why it is asked: it shows whether you can build and ship what you write. Read the full explanation: Maven build lifecycle guide.

Follow-up questions

What Java interview questions are asked for 10 years of experience?

The topics are the same as above, asked for the reasoning behind them: the happens-before rule, how a HashMap resizes, which collector a service runs and why, and when virtual threads beat a thread pool. Answer with a trade-off and a result you measured. The interview loop guide shows how senior rounds fit together.

What Java interview questions should I prepare for software developer roles?

Start with the groups above: strings and equality, collections, memory, threads and one framework such as Spring. For each behaviour, write a five-line program, predict its output, then run it and say aloud why it behaves that way. The coding interview guide covers the problem-solving round that sits beside these questions.

Are Java 8 interview questions still asked, and which Java version should I study?

Yes: lambdas, streams and Optional arrived in Java 8 and are the baseline of most code. Study the version the job uses. Most teams run a long-term-support (LTS) release, and Oracle lists Java SE 8, 11, 17, 21 and 25 as LTS releases (Oracle Java SE support roadmap, read October 2026). Records were finalised in Java 16, so they are in every LTS from 17 on, and virtual threads in Java 21 (JEP 395, JEP 444).

What are the most common Java interview questions on threads and collections?

For threads: synchronized against volatile, why a counter loses updates, and what virtual threads change, as in the concurrency group above. For collections: ArrayList against LinkedList, how a HashMap works and HashMap against ConcurrentHashMap. See concurrency interview questions, and for web services built on representational state transfer (REST) and application programming interfaces (APIs), REST API interview questions.

Key takeaways
  • Use equals for strings and boxed numbers; == compares references.
  • Override equals and hashCode together, or hash collections break.
  • Overloads are chosen at compile time, overrides at run time.
  • synchronized gives exclusion and visibility; volatile gives visibility but not atomicity.
  • Streams are lazy until a terminal operation; virtual threads make blocking cheap.

Sources

  1. String (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  2. Integer.valueOf (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  3. Object: equals and hashCode (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  4. The Kinds of Exceptions (Java Language Specification, SE 21, section 11.1.1) — Oracle (date = read date), 24 Oct 2026
  5. The try-with-resources statement (The Java Tutorials) — Oracle (date = read date), 24 Oct 2026
  6. JEP 395: Records — OpenJDK, 3 Sep 2026
  7. ArrayList (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  8. LinkedList (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  9. ConcurrentHashMap (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  10. java.util.stream package summary (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  11. Run-time data areas (Java Virtual Machine Specification, SE 21, chapter 2) — Oracle (date = read date), 24 Oct 2026
  12. Garbage-First (G1) garbage collector (Java garbage collection tuning guide) — Oracle (date = read date), 24 Oct 2026
  13. volatile fields (Java Language Specification, SE 21, section 8.3.1.4) — Oracle (date = read date), 24 Oct 2026
  14. JEP 444: Virtual Threads — OpenJDK, 30 Oct 2025
  15. Introduction to the Spring IoC container and beans (Spring Framework reference) — Spring (VMware Tanzu; date = read date), 24 Oct 2026
  16. Auto-configuration (Spring Boot reference) — Spring (VMware Tanzu; date = read date), 24 Oct 2026
  17. Introduction to the Build Lifecycle (Apache Maven) — Apache Software Foundation, 21 Oct 2026
  18. HashMap (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  19. JEP 180: Handle Frequent HashMap Collisions with Balanced Trees — OpenJDK, 14 Jun 2017
  20. Compile-Time Checking of Exceptions (Java Language Specification, SE 21, section 11.2) — Oracle (date = read date), 24 Oct 2026
  21. Happens-before order (Java Language Specification, SE 21, section 17.4.5) — Oracle (date = read date), 24 Oct 2026
  22. Interface method declarations (Java Language Specification, SE 21, section 9.4) — Oracle (date = read date), 24 Oct 2026
  23. abstract classes (Java Language Specification, SE 21, section 8.1.1.1) — Oracle (date = read date), 24 Oct 2026
  24. Compile-Time Step 2: Determine Method Signature (Java Language Specification, SE 21, section 15.12.2) — Oracle (date = read date), 24 Oct 2026
  25. Run-Time Evaluation of Method Invocation (Java Language Specification, SE 21, section 15.12.4) — Oracle (date = read date), 24 Oct 2026
  26. StringBuilder (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  27. Optional (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  28. java.util.function package summary (Java SE 21 API documentation) — Oracle (date = read date), 24 Oct 2026
  29. Oracle Java SE support roadmap — Oracle (date = read date), 24 Oct 2026