Java & Spring Boot Interview Questions
Q: What is the difference between == and equals() in Java?
== compares object references (memory address) for objects, or values for primitives. equals() compares object content (overridden in String, Integer, etc. to compare values). Always use equals() to compare String values. Use == only for reference equality or primitives.
Q: What is the difference between ArrayList and LinkedList?
ArrayList uses a dynamic array — O(1) random access, O(n) insertion/deletion in the middle. LinkedList uses a doubly-linked list — O(1) insertion/deletion at head/tail, O(n) random access. In practice, ArrayList is almost always faster due to cache locality. Use LinkedList only when you frequently insert/delete at both ends.
Q: What is Spring Dependency Injection?
Spring IoC container manages the lifecycle of beans (@Component, @Service, @Repository, @Controller). It injects dependencies via constructor injection (preferred), setter injection, or field injection (@Autowired). Constructor injection is preferred because it makes dependencies explicit, enables immutability (@RequiredArgsConstructor), and simplifies testing.
Q: What is @Transactional?
Marks a method or class as transactional — Spring wraps it in a database transaction. If the method completes normally, the transaction commits. If a RuntimeException is thrown, it rolls back. Use readOnly=true for read-only queries (performance hint). Propagation controls what happens when transactional methods call each other (REQUIRED, REQUIRES_NEW, NESTED, etc.).
Q: What is the JVM and how does garbage collection work?
The JVM executes Java bytecode. Garbage collection automatically reclaims memory for unreachable objects. The heap is divided into generations: Young (new objects, GC is frequent/fast), Old/Tenured (long-lived objects, less frequent). G1GC (default Java 9+) divides the heap into regions and tries to meet pause-time goals. Avoid premature optimization — tune GC only when profiling shows it's a bottleneck.
Keep your own version of these notes — editable, searchable, and organised by your stack.
Start free