interviewpath
  • 0day streak
  • 10total XP
  • Level 1
0/5

Unit 2 · Software engineering · Lesson 10 of 15

Java essentials

When Java is your interview language, interviewers expect fluent use of the collections framework, correct equals and hashCode, an understanding of what generics erase at runtime, immutable value types, java.util.concurrent instead of hand-rolled threading, and basic JVM memory and garbage-collection behavior.
About 3 hFocus mode

Step 1: Learn the ideas

  • Collections framework: List, Set, Map, Deque, PriorityQueue
  • equals/hashCode contract and records
  • Generics, wildcards and type erasure
  • Immutability: final fields, unmodifiable collections, records
  • java.util.concurrent: executors, futures, concurrent collections, atomics
  • volatile, synchronized and happens-before
  • Virtual threads (Java 21 and later)
  • JVM heap, stacks and generational garbage collection

Why it matters

Choosing Java for an interview signals that you write production-style Java. The common failures are semantic rather than syntactic: a HashMap key whose hash changes after insertion, a counter made "thread-safe" with volatile, a generic method that assumes type information exists at runtime, or a cache that leaks because its entries stay reachable.

Core ideas

  • Collections. Program to interfaces (List, Map, Deque). ArrayList gives O(1) indexed access; ArrayDeque is the usual stack and queue; HashMap has expected O(1) operations and no order; LinkedHashMap keeps insertion or access order, which makes a simple LRU cache; TreeMap keeps keys sorted with O(log n) operations and range queries; PriorityQueue is a binary heap ordered by natural order or a comparator. Iterators of the general-purpose implementations are fail-fast on a best-effort basis: a structural change during iteration usually throws ConcurrentModificationException.
  • equals and hashCode. Equal objects must have equal hash codes, and both should depend only on fields that do not change while the object sits in a hash-based collection. == compares references, so compare boxed numbers and strings with equals. Records generate consistent equals, hashCode and toString from their components.
  • Generics and erasure. Type arguments exist only at compile time; at runtime List<String> and List<Integer> share one class. You cannot test instanceof List<String>, create new T[n], or overload methods that differ only in type arguments. Use ? extends T for parameters you only read from and ? super T for parameters you only write to.
  • Immutability. final stops reassignment of a field, not mutation of the object it refers to. Immutable classes (final fields, no setters, defensive copies, or records with immutable components) are easy to share between threads. List.of builds an unmodifiable list; Collections.unmodifiableList is a read-only view of a list that can still change underneath.
  • Concurrency. Prefer executors to hand-managed threads, CompletableFuture for composing asynchronous results, and ConcurrentHashMap, AtomicLong or LongAdder for shared state. Since Java 21, virtual threads make thread-per-task code cheap for blocking I/O. volatile guarantees visibility and ordering of individual reads and writes but does not make count++ atomic; synchronized and the java.util.concurrent locks provide mutual exclusion plus happens-before edges.
  • JVM memory. Objects live on the garbage-collected heap; each thread's stack holds frames with local primitives and references. HotSpot's collectors are generational because most objects die young, and G1 is selected by default on most configurations. A Java leak is an object that stays reachable but is no longer needed, such as entries in a static map or listeners never removed.

Common misconceptions

  • "volatile makes increments thread-safe." Each read and write is visible, but count++ is still a read-modify-write race; use an atomic class or a lock.
  • "final means immutable." It fixes the reference, not the state of the referenced object.
  • "Generic types are checked at runtime." They are erased; raw types and unchecked casts can put a wrong element into a collection, which then fails with a ClassCastException far from the cause.
  • "Garbage collection prevents memory leaks." It reclaims only unreachable objects; reachable but useless objects accumulate.

How to approach it

Write small programs that demonstrate each failure (a mutated HashMap key, a lost update under volatile) and then fix them. Mid-level and senior backend conversations may extend into thread-pool sizing, CompletableFuture error handling and garbage-collection pauses.

Suggested order

  1. 1Read the dev.java "The Collections Framework" tutorial and write a one-page table of the implementations above with ordering guarantees and operation costs.
  2. 2Read the equals and hashCode entries of the java.lang.Object API page; use a class with a mutable field as a HashMap key, mutate it after insertion, explain why lookup fails, then convert the class to a record.
  3. 3Read the dev.java "Type Erasure" page; explain why new T[n] and instanceof List<String> do not compile, and write one method each with ? extends T and ? super T.
  4. 4Read the "High Level Concurrency Objects" part of the Concurrency tutorial and the java.util.concurrent package summary; rewrite a raw-thread program with an ExecutorService and CompletableFuture, including shutdown.
  5. 5Read section 17.4 (Memory Model) of JLS chapter 17 for the happens-before rules of volatile, locks and thread start/join; skip the formal execution model.
  6. 6Read "Garbage Collector Implementation" and "Available Collectors" in the HotSpot GC Tuning Guide (Release 25).
  7. 7Solve LRU Cache with LinkedHashMap and again with a HashMap plus doubly linked list; solve Print FooBar Alternately and Building H2O with java.util.concurrent primitives.

Step 2: Check off what you can do

Tap each one you can genuinely do or explain. Each earns 10 XP.

Step 3: Go deeper

  • Dev.java: Learn JavaStart here

    Oracle (dev.java)

    Section: The Collections Framework

    Collection interfaces and implementations; continue with the Type Erasure, Records and Virtual Threads tutorials listed under this resource.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • Java SE 25 API documentation

    Oracle (docs.oracle.com)

    Section: java.lang.Object: equals and hashCode

    The exact equals and hashCode contracts; the java.util.concurrent package summary for executors, synchronizers and memory-consistency rules.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • The Java Tutorials: Concurrency lesson

    Oracle (docs.oracle.com)

    Section: High Level Concurrency Objects

    Concepts behind lock objects, executors, concurrent collections and atomic variables (written for JDK 8).

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • The Java Language Specification, Java SE 25 Edition: Chapter 17, Threads and Locks

    Oracle (docs.oracle.com)

    Section: 17.4. Memory Model

    Happens-before rules for volatile, locks, thread start and join; read the informal parts and examples.

    Open(opens in a new tab)Link responded, 5 Oct 2026
  • HotSpot Virtual Machine Garbage Collection Tuning Guide (Java SE 25)

    Oracle (docs.oracle.com)

    Section: Garbage Collector Implementation

    Generations and the weak generational hypothesis; then Available Collectors for when Serial, Parallel, G1 or ZGC fits.

    Open(opens in a new tab)Link responded, 5 Oct 2026

Step 4: Practice

Write every solution in Java and use the language deliberately: LinkedHashMap in access order for LRU Cache (then a hand-built version), a bucket array whose keys rely on equals and hashCode for Design HashMap, PriorityQueue with a comparator for Top K, and Semaphore, ReentrantLock with Condition, or CyclicBarrier for the concurrency problems. Extra exercise outside LeetCode: write an immutable value class and the equivalent record, and test both as HashMap keys and across threads.

Record attempts on each problem’s page (+15 XP, +10 more when solved on your own).

Step 5: Test yourself

Answer each one out loud or on paper before you finish the lesson.

  1. Question 1When would you choose TreeMap over HashMap?
  2. Question 2What goes wrong if two equal objects have different hash codes?
  3. Question 3Why can you not write new T[10] in a generic class?
  4. Question 4What does ? super T allow you to do with a parameter?
  5. Question 5Is a final List<String> field immutable?
  6. Question 6What makes an object eligible for garbage collection?

You’re done when you can

  • Explain the equals/hashCode contract and demonstrate the mutated-key bug in a short program.
  • Explain type erasure and two compile errors it causes, without notes.
  • Rewrite a thread-based program with an ExecutorService and explain how its tasks are shut down.
  • Explain what volatile guarantees and why it does not make an increment atomic.

Finish the lesson

How confident do you feel?

Lesson complete!

+50 XP

Reviews will come up on the Practice tab so it sticks.

Your notes and study time

Log study time