liminfo

Java Reference

Free reference guide: Java Reference

40 results

About Java Reference

The Java Reference is a comprehensive, searchable cheat sheet covering the Java programming language from fundamentals to modern features. It is organized into eight categories: Basics (variables, type inference with var, control flow, arrays, text blocks, and pattern matching instanceof), Classes (inheritance, abstract classes, enums, records, sealed classes, and inner classes), Interfaces (functional interfaces, default and static methods, and lambda expressions), Generics (generic classes, generic methods, and wildcards), Collections (List, Map, Set, Queue, Deque, and utility methods), Streams (stream creation, filter/map/collect, reduce, and Optional), Concurrency (Thread, ExecutorService, CompletableFuture, synchronized, volatile, and AtomicInteger), and Exceptions (try/catch/finally, throws, custom exceptions, and try-with-resources).

This reference is designed for Java developers at all levels — from beginners learning the language to senior engineers who need a quick reminder of an API signature or a modern Java feature. The code examples are self-contained and executable, covering both traditional patterns and modern idioms introduced in Java 10 through Java 17, including var type inference, record classes, sealed classes, and the enhanced switch expression.

Each entry includes the syntax name, a plain-language description, and a complete code example. The eight-category structure matches the mental model most Java developers use: start with language fundamentals, move through object-oriented constructs, then tackle the standard library topics of generics, collections, streams, concurrency, and error handling. This makes the reference equally useful for daily development, code review, and technical interview preparation.

Key Features

  • var type inference (Java 10), text blocks (Java 13), and pattern matching instanceof (Java 16) examples
  • record classes (Java 16) and sealed classes (Java 17) with complete syntax demonstrations
  • Enhanced switch expression with arrow syntax for concise conditional assignments
  • Collections framework: ArrayList, HashMap, HashSet, LinkedList, ArrayDeque with common operations
  • Stream API: filter, map, collect, reduce, partitioningBy, and Stream.toList() (Java 16)
  • CompletableFuture chaining with supplyAsync, thenApply, thenAccept, and exceptionally
  • Concurrency primitives: synchronized methods, volatile fields, AtomicInteger, and ExecutorService thread pools
  • try-with-resources for automatic resource management and custom exception class patterns

Frequently Asked Questions

What is the difference between var, final, and a regular variable declaration in Java?

var (Java 10) infers the type from the right-hand side at compile time and is only valid for local variables. final declares a constant — the variable is assigned once and cannot be reassigned. A regular typed declaration (e.g., int x = 5) works anywhere and is fully explicit about the type.

When should I use a record class vs a regular class in Java?

A record (Java 16) is ideal for immutable data carriers where you only need to store and retrieve values. It automatically generates constructor, equals, hashCode, and toString. Use a regular class when you need mutable state, complex constructors, inheritance, or custom logic beyond simple data storage.

What is the difference between map() and flatMap() in the Stream API?

map() applies a function to each element and returns a stream of the results. flatMap() applies a function that returns a stream for each element and then flattens all those streams into a single stream. Use flatMap() when each element maps to multiple values or a nested collection.

How does CompletableFuture differ from Future?

Future provides a blocking get() method to retrieve the result of an async computation. CompletableFuture adds non-blocking callback chaining (thenApply, thenAccept, thenCompose), the ability to combine multiple futures (allOf, anyOf), and built-in error handling (exceptionally, handle). It is the recommended approach for async code in modern Java.

What is the difference between synchronized and volatile in Java?

synchronized ensures that only one thread executes a block at a time (mutual exclusion and visibility). volatile ensures that reads and writes to a single variable are visible to all threads immediately, but does not provide mutual exclusion. For compound operations like increment (read-modify-write), use synchronized or AtomicInteger instead of volatile.

What is a sealed class and why would I use one?

A sealed class (Java 17) restricts which classes can extend it using a permits clause. This is useful for modeling closed type hierarchies — for example, a Shape that can only be Circle or Rectangle. Sealed classes work well with pattern matching in switch expressions to enable exhaustive case handling at compile time.

What is try-with-resources and when should I use it?

try-with-resources (Java 7) automatically closes any resource that implements AutoCloseable at the end of the try block, even if an exception is thrown. Use it whenever you open streams, readers, writers, database connections, or other resources that need to be closed to prevent resource leaks.

What is the difference between List.of(), Arrays.asList(), and new ArrayList()?

List.of() returns an immutable list that throws UnsupportedOperationException on add/remove. Arrays.asList() returns a fixed-size list backed by the array — you can set elements but not add or remove. new ArrayList() returns a fully mutable list. Choose based on whether mutability is needed.