Java 8 and Advanced Features
Introduction
Java 8 marked a turning point in the language’s evolution. It introduced functional programming constructs, a modern Date/Time API, and powerful concurrency tools. This guide explores Java 8 and beyond in exhaustive detail, with examples, diagrams, and interview-ready notes.
Lambda Expressions
Lambdas allow you to write concise code by passing behavior as parameters. They are the foundation of functional programming in Java.
List names = Arrays.asList("Alice", "Bob", "Charlie");
names.forEach(name -> System.out.println(name));
Functional Interfaces
Java 8 introduced functional interfaces like Predicate, Function, Consumer, and Supplier.
These are used extensively with lambdas and streams.
Streams API
Streams provide a declarative way to process collections. They support operations like filtering, mapping, reducing, and can run in parallel.
List numbers = Arrays.asList(1,2,3,4,5);
int sum = numbers.stream()
.filter(n -> n % 2 == 0)
.mapToInt(n -> n)
.sum();
System.out.println(sum); // 6
Optional Class
Optional helps avoid NullPointerException by providing a container object that may or may not contain a value.
Default and Static Methods in Interfaces
Interfaces can now have default and static methods, enabling backward compatibility and richer APIs.
Method References
Method references provide shorthand for lambdas that call existing methods. Types: static, instance, and constructor references.
Date and Time API (java.time)
Java 8 introduced a new Date/Time API inspired by Joda-Time. Classes include LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Period, and Duration.
Nashorn JavaScript Engine
Java 8 included Nashorn, a JavaScript engine that allows running JS code inside Java applications.
CompletableFuture
CompletableFuture enables asynchronous programming with pipelines, combining futures, and exception handling.
Collectors and Advanced Stream Operations
Collectors provide powerful ways to group, partition, and summarize data from streams.
Concurrency Enhancements
Java 8 introduced StampedLock, LongAdder, and enhanced CompletableFuture for better concurrency control.
Other Java 8 Enhancements
- Type annotations
- Repeating annotations
- Base64 API
Advanced Features Beyond Java 8
- Java 9: Modules
- Java 10: Local variable type inference (
var) - Java 11: New APIs and HTTP Client
- Java 14: Records
- Java 17: Sealed classes
- Java 21: Virtual threads (Project Loom)
Best Practices
- Write clean functional code.
- Avoid misuse of streams (don’t overuse parallel streams).
- Balance readability and performance.
Interview-Ready Notes
- Q: Difference between streams and collections?
A: Collections store data; streams process data. - Q: Why Optional?
A: To avoid null checks and NullPointerException. - Q: Lambda vs anonymous class?
A: Lambdas are concise and focus on behavior, anonymous classes are verbose. - Q: What is CompletableFuture?
A: A class for async programming with chaining and combining tasks.
Conclusion
Java 8 transformed the language by introducing functional programming, modern APIs, and concurrency enhancements. Mastering these features is essential for writing efficient, maintainable, and modern Java applications. Beyond Java 8, newer versions continue to evolve with records, sealed classes, and virtual threads, ensuring Java remains relevant in modern software development.