- Java Basics
- OOP Concepts
- Collections
- Exception Handling
- Multithreading & Concurrency
- Streams & Lambda Expressions
- Java 8/11 Features
- JVM & Memory Management
- Garbage Collection
- JDBC
- Production-Level Questions
- Best Practices
- Java Commands / Tools Cheat Sheet
Java Basics
- JVM runs bytecode, JRE provides JVM + libraries, JDK provides JRE + dev tools.
==compares references,.equals()compares object content.
- byte, short, int, long, float, double, char, boolean.
final: constants or prevent inheritance/overriding.finally: block executed after try-catch.finalize(): called before GC destroys an object.
- String: immutable.
- StringBuilder: mutable, fast, not thread-safe.
- StringBuffer: mutable, thread-safe, slower.
- Security, thread-safety, caching in String pool.
- Compiler uses
StringBuilderinternally for+operations.
OOP Concepts
- One object can take many forms; includes overloading (compile-time) and overriding (runtime).
- Abstract class: methods with/without implementation, constructors, fields.
- Interface: method declarations (Java 8+ allows default/static methods), supports multiple inheritance.
- Wrapping data and methods in a class; control access with getters/setters.
- Mechanism to derive a class from another to reuse code.
- Overloading: same name, different params, compile-time.
- Overriding: subclass changes parent method behavior, runtime.
Collections
- List: ordered, allows duplicates.
- Set: unordered, no duplicates.
- Map: key-value, unique keys.
- HashMap: not synchronized, allows null keys/values.
- Hashtable: synchronized, no null keys/values.
- ConcurrentHashMap: thread-safe, efficient concurrency.
- ArrayList: index-based, fast random access.
- LinkedList: node-based, fast insert/remove.
- Fail-fast: throws ConcurrentModificationException (ArrayList).
- Fail-safe: works on clone, no exception (ConcurrentHashMap).
Exception Handling
- Checked: compiler-enforced (IOException).
- Unchecked: runtime errors (NullPointerException).
- Extend Exception for checked, RuntimeException for unchecked.
- Auto-closes resources like files or streams.
- throw: explicitly throw exception.
- throws: declare exceptions a method may throw.
Multithreading & Concurrency
- synchronized: mutual exclusion + visibility.
- volatile: visibility only, no locking.
- Manages thread pool and task submission efficiently.
- Runnable: no return, cannot throw checked exceptions.
- Callable: returns result, can throw checked exceptions.
- Acquire locks in consistent order, minimize scope, use tryLock.
- Thread-local variable; each thread has its own copy.
Streams & Lambda Expressions
- Functional processing of collections; supports map, filter, reduce.
- map: one-to-one mapping.
- flatMap: flattens nested collections.
- Container that may/may not hold a value; avoids NullPointerException.
(int a, int b) -> a + bJava 8/11 Features
Default vs static methods-
Default: method with implementation in interface.
-
Static: belongs to interface, not instance.
Functional interface
- Interface with single abstract method (Runnable, Callable, Comparator).
LocalDate vs Date
-
LocalDate: immutable, thread-safe.
-
Date: mutable, older API.
parallelStream
- Allows multi-core processing automatically.
JVM & Memory Management
JVM memory areas- Heap, Stack, Metaspace, Code Cache, PC Registers.
Stack vs Heap
-
Stack: method calls, primitives, short-lived.
-
Heap: objects, garbage collected.
ClassLoader
- Loads classes into JVM at runtime (Bootstrap, Extension, Application).
Garbage Collection
How GC works- Frees memory of unreachable objects automatically.
GC algorithms
- Serial, Parallel, CMS, G1.
Forcing GC
- System.gc() requests GC (not guaranteed immediately).
⚡ Memory leaks
- Objects unintentionally referenced prevent GC.
JDBC
Connecting to DB- DriverManager.getConnection(url, user, pass) or DataSource.
Statement vs PreparedStatement vs CallableStatement
-
Statement: simple SQL, prone to injection.
-
PreparedStatement: precompiled, safer.
-
CallableStatement: calls stored procedures.
Transaction management
- Use commit() and rollback() for atomicity.
Production-Level Questions
⚡ Detect memory leaks- Use heap dump analysis with VisualVM/Eclipse MAT.
Performance improvement with collections
- Choose proper data structures, avoid unnecessary synchronization.
Concurrency issues
- Use Concurrent collections, locks, atomic operations.
GC tuning
- Pick GC type based on workload (G1: low-latency, Parallel: throughput).
⚡ Debug slow apps
- Profile, analyze thread dumps, GC logs, monitor performance.
Best Practices
-
Prefer immutability (final) for thread safety.
-
Use StringBuilder for heavy string ops.
-
Always close resources (try-with-resources).
-
Avoid unnecessary object creation in loops.
-
Use Concurrent collections for multi-threaded apps.
-
Handle exceptions gracefully.
-
Write unit tests.
-
Follow proper naming conventions and document code.
Java Commands / Tools Cheat Sheet
-
javac FileName.java → Compile Java file.
-
java ClassName → Run compiled class.
-
jar cf app.jar *.class → Create JAR file.
-
jar xf app.jar → Extract JAR.
-
javadoc FileName.java → Generate documentation.
-
jdb ClassName → Debug Java app.
-
java -Xmx512m ClassName → Set max heap size.
-
java -version → Check Java version.