Java Fundamentals Flashcards
Java Fundamentals Flashcards
30 interactive flashcards spanning JVM internals to modern Java features. Press Space to flip, rate 1-4.
Additional Flashcard Topics
JVM Memory Model: heap (objects), stack (local variables), method area (class data). Garbage collection reclaims heap memory. Young/Old generation generational collection. The JVM uses mark-and-sweep algorithms to identify unreachable objects.
Generics:
List<String>enforces type safety at compile time. Type erasure removes generic types at runtime. Wildcards:? extends T(covariant),? super T(contravariant). PECS: Producer Extends, Consumer Super.Streams API:
list.stream().filter(x -> x > 0).map(x -> x * 2).collect(Collectors.toList()). Functional-style operations on collections. Parallel streams use fork-join pool for automatic parallelisation.Records (Java 16+):
record Point(int x, int y)— immutable data carriers with auto-generated constructor, accessors, equals, hashCode, toString. Records are implicitly final and extend Record.Sealed Classes (Java 17+):
sealed class Shape permits Circle, Rectangle— restricted hierarchies enabling exhaustive pattern matching. Sealed classes restrict which classes can extend them.Pattern Matching (Java 21+):
switch (shape) { case Circle c -> ... }— pattern matching for switch eliminates explicit casts and null checks.
Intuition
Java is a class-based, object-oriented language that runs on the JVM — code compiles to platform-independent bytecode. The JVM handles memory management via garbage collection, so you don’t manually free memory. Java’s type system is statically checked at compile time, with single class inheritance but multiple interface implementation. Modern Java (8+) embraces functional programming with lambdas and streams. Java’s “write once, run anywhere” philosophy and massive ecosystem make it a perennial choice for enterprise applications. The JVM’s maturity and optimisation make Java performant for most workloads.
Common Pitfalls
- equals/hashCode contract: Overriding
equals()without overridinghashCode()breaks hash-based collections (HashMap, HashSet) — always override both together. The contract requires that equal objects have equal hash codes. - Autoboxing overhead: Java automatically converts between primitives and wrapper types (
int↔Integer) — this creates unnecessary object allocations in loops, hurting performance. Use primitive types in performance-critical code. - Finalizer pitfalls: Using
finalize()for cleanup is deprecated — it’s slow, unpredictable, and can cause resurrection of objects. Usetry-with-resourcesandAutoCloseableinstead. - ConcurrentModificationException: Modifying a collection while iterating over it (e.g.,
for (Item i : list) list.remove(i)) throws CME. UseIterator.remove()orremoveIf(). - Null vs Optional: Java has both
nullandOptional<T>.Optionalis not serializable and should not be used as fields — use it only for return types to indicate possible absence.
Cross-References
- Java Fundamentals Practice: Auto-graded problems testing the same core Java concepts covered in these flashcards.
- Types and Variables: Primitive and reference type fundamentals.
- Control Flow: If-else, switch, and loop constructs.
- Kotlin Basics: Kotlin runs on the JVM and is more concise than Java.
- Go Standard Library: Go’s approach to concurrency and standard library compared to Java.