What Are Method References? Method references are a signal to the reader: “this lambda does exactly one thing — it calls this existing method.” When the entire body of a lambda is a single method call, replacing it with a method reference removes noise without losing information. Knowing which of the four kinds to apply in each context is what separates fluent Java 8 code from code that merely uses the syntax.
Continue reading »Java 8 Tutorial
16 posts in this section
New APIs: Base64, StringJoiner, Spliterator, Files.lines, StampedLock, Nashorn
Base64 Before Java 8, Base64 encoding required a third-party library (Apache Commons Codec, Guava) or the internal sun.misc.BASE64Encoder class — an API that was never officially supported and was deliberately restricted from Java 9 onwards. Java 8 standardised encoding in java.util.Base64, which covers standard, URL-safe, and MIME variants and can wrap streams for large payloads. Java 8 added java.util.Base64 to the standard library. Three Encoders // Standard Base64 (RFC 4648) Base64.
Continue reading »Optional: Eliminating NullPointerException the Right Way
The Problem Optional Solves NullPointerException is the most common runtime exception in Java, and it is almost always avoidable. Optional<T> is not a magic fix — used incorrectly it becomes a more verbose null check. Used correctly, it encodes the possibility of absence directly in the type system, so callers can never “forget” to handle the empty case. This article covers both how to use Optional and — critically — how not to.
Continue reading »Parallel Streams: ForkJoinPool, Spliterators, and When NOT to Parallelize
How Parallel Streams Work Parallel streams are one of Java 8’s most misused features. It is tempting to add .parallel() to any slow stream pipeline, but the performance characteristics are counterintuitive: parallel can make things slower for small data, and adding blocking I/O inside a parallel stream can stall the entire JVM. This article explains the mechanics, the cases where parallel genuinely helps, and the patterns to avoid. A parallel stream splits its source into sub-sequences, processes each sub-sequence on a separate thread, and merges the results.
Continue reading »Setting Up Java 8: JDK Options, Maven/Gradle, and IDE Configuration
Choosing a Java 8 Distribution Getting your environment right before writing a single line of code saves hours of debugging compiler errors and version conflicts later. This article covers every distribution option, all three major build tools, and both popular IDEs — including the specific settings that trip up most developers when first configuring Java 8. Oracle stopped providing free Java 8 updates for commercial use in January 2019. Multiple vendors now ship free, production-quality Java 8 builds:
Continue reading »Streams API: Lazy Pipelines and the Functional Data Model
The Problem Streams Solve Java loops are not wrong — they are often the right tool. But when a computation is a multi-step pipeline of filter → transform → aggregate, a loop hides the structure of the computation in a tangle of temporary variables and mutated collections. The Streams API makes the structure explicit, lets the JVM optimise it, and composes naturally with lambdas and method references. Consider filtering a list of orders to find the names of active premium customers who spent over $500, sorted alphabetically:
Continue reading »