13 Core Java
13 Core Java
Overview
Given your JVM Trainee applications, a sharp grasp of Java internals — memory model, collections, and
exception handling — will differentiate you from candidates with only surface-level syntax knowledge.
Key Concepts
• JVM, JRE, JDK: JVM executes bytecode and is platform-specific; JRE bundles the JVM with core libraries
needed to run Java programs; JDK includes JRE plus development tools (compiler, debugger) needed to
build Java programs.
• Memory Areas: Heap vs Stack: Objects live on the heap (shared, garbage-collected); method calls, local
variables, and references live on the stack (thread-specific, LIFO, automatically reclaimed on method
return).
• Garbage Collection: Automatic memory management that reclaims heap memory occupied by unreachable
objects; generational GC (Young/Old generation) exploits the observation that most objects die young.
• Collections Framework: List (ArrayList, LinkedList — ordered, allows duplicates), Set (HashSet, TreeSet —
unique elements), Map (HashMap, TreeMap — key-value pairs) — each with different performance
characteristics for access/insertion/ordering.
• HashMap Internals: Uses an array of buckets with hashCode() to locate a bucket and equals() to resolve
collisions (via linked list, converting to a red-black tree for large buckets in Java 8+); average O(1) get/put.
• Exception Handling: Checked exceptions must be declared/handled (e.g., IOException); unchecked
exceptions (RuntimeException subclasses) don't require explicit handling; try-with-resources auto-closes
resources implementing AutoCloseable.
• String Pool & Immutability: String literals are interned in a special pool for reuse; Strings are immutable, so
every 'modification' creates a new object — this enables safe sharing and consistent hashing.
• equals() and hashCode() Contract: Objects that are equal must have the same hashCode(); overriding one
without the other breaks correctness in hash-based collections like HashMap/HashSet.
• Generics: Enable type-safe, reusable code (e.g., List<String>) with compile-time type checking, using type
erasure at runtime for backward compatibility.
• Functional Interfaces & Lambdas: Java 8 introduced lambdas and functional interfaces (Runnable,
Comparator, Function<T,R>) enabling more concise, functional-style code, plus the Stream API for
declarative collection processing.
Common Interview Questions
Q1. Why is String immutable in Java?
Immutability enables safe sharing via the string pool (saving memory), guarantees thread safety without
synchronization, allows consistent caching of the hash code for fast use in hash-based collections, and
prevents security issues from mutable strings used as parameters (e.g., in file paths, class loading).
Q3. Explain the difference between checked and unchecked exceptions with examples.
Checked exceptions (like IOException, SQLException) are checked at compile time and must be declared or
caught; unchecked exceptions (like NullPointerException, ArrayIndexOutOfBoundsException, extending
RuntimeException) aren't required to be declared and typically indicate programming bugs.
Q5. What is the difference between ArrayList and LinkedList in terms of performance?
ArrayList offers O(1) indexed access but O(n) insertion/deletion in the middle (due to shifting elements);
LinkedList offers O(1) insertion/deletion given a node reference but O(n) indexed access since it must
traverse from the head/tail.
Q7. Explain try-with-resources and why it's preferred over manual finally blocks.
try-with-resources automatically closes any resource implementing AutoCloseable at the end of the block,
even if an exception occurs, without needing an explicit finally block — reducing boilerplate and preventing
resource leaks from forgotten close() calls.