☕ Java Interview Questions
Master Java interviews with questions on Core Java, Spring Framework, JVM internals, and design patterns
Explain the difference between String, StringBuilder, and StringBuffer in Java
MediumString: Immutable class. Once created, the value cannot be changed. Any modification creates a new String object. Thread-safe because it's immutable.
StringBuilder: Mutable class. Values can be modified without creating new objects. Not thread-safe, but faster than StringBuffer.
StringBuffer: Mutable class like StringBuilder, but thread-safe (synchronized methods). Slower than StringBuilder due to synchronization overhead.
What are the main features of Java 17 and Java 21 (LTS versions)?
MediumJava 17 LTS Features:
- Sealed Classes - restrict which classes can extend/implement them
- Pattern Matching for switch (Preview)
- Text Blocks for multi-line strings
- Records - compact syntax for data classes
- Enhanced Pseudo-Random Number Generators
Java 21 LTS Features:
- Virtual Threads (Project Loom) - lightweight threads for high concurrency
- Sequenced Collections - new interfaces for collections with defined order
- Pattern Matching for switch (Standard)
- Record Patterns - deconstruct record values
- String Templates (Preview)
Explain the Singleton Design Pattern and implement it thread-safe
HardThe Singleton pattern ensures a class has only one instance and provides a global point of access to it. It's commonly used for logging, driver objects, caching, and thread pools.
Thread-Safe Implementations:
How does the Java Garbage Collector work? Explain G1GC.
HardJava's Garbage Collector automatically manages memory by reclaiming objects that are no longer reachable. The G1 (Garbage-First) GC is the default in Java 9+ and is designed for applications with large heaps.
G1GC Key Concepts:
- Region-based: Heap is divided into equal-sized regions
- Generational: Still uses Young/Old generation concepts
- Concurrent: Most work happens concurrently with application threads
- Predictable pause times: Aims to meet pause time goals
G1GC JVM Flags:
Explain CompletableFuture and asynchronous programming in Java
MediumCompletableFuture is a powerful class for asynchronous programming, introduced in Java 8. It allows you to write non-blocking code and chain multiple asynchronous operations.
What is the difference between HashMap and ConcurrentHashMap?
HardHashMap: Not thread-safe. Multiple threads modifying it can cause inconsistencies or infinite loops.
ConcurrentHashMap: Thread-safe without synchronizing the entire map. Uses lock striping for better concurrency.
Explain Java Stream API and its common operations
MediumJava Stream API provides a functional approach to process collections. Streams support lazy evaluation, parallel processing, and can significantly reduce boilerplate code.
What are Java Generics and why are they important?
MediumGenerics enable types (classes and interfaces) to be parameters when defining classes, interfaces, and methods. They provide compile-time type safety and eliminate the need for casting.
Explain Java Reflection and when to use it
HardReflection allows inspection and modification of classes, interfaces, fields, and methods at runtime. It's used by frameworks like Spring, Hibernate, and JUnit for dependency injection, ORM, and testing.
What is the difference between == and equals() in Java?
Easy== compares object references (memory addresses) for objects, and values for primitives.equals() compares the actual content/values of objects.
Explain Java Exception Handling and best practices
MediumJava exceptions are divided into checked exceptions (must be handled), unchecked exceptions (RuntimeException), and errors. Proper exception handling improves code reliability and maintainability.
Interview Tips for Java
- ✓ Be prepared to discuss JVM internals, memory management, and GC
- ✓ Know the difference between JDK, JRE, and JVM
- ✓ Understand collections framework deeply (List, Set, Map implementations)
- ✓ Practice multithreading, synchronization, and concurrent collections
- ✓ Be familiar with Spring Framework and dependency injection
- ✓ Know modern Java features (Streams, Lambdas, Records, Sealed Classes)