Java Reference ยท Language Features by Version
Java 8 features
A tour of what Java 8 (2014) added: lambdas, method references, the Stream API, default methods, Optional, java.time, and what changed in CompletableFuture and Map.
Java 8 (March 2014, LTS) was the biggest change to the language since generics. It brought functional programming to Java: lambda expressions, method references, the Stream API and default methods on interfaces. It also replaced the awkward date classes with java.time, added Optional and made concurrency easier with CompletableFuture.
Almost every modern Java codebase assumes Java 8 as a minimum. The sections below show each feature with a short, runnable example, and the table at the end lists the main additions with their packages.
List<String> names = Arrays.asList("ann", "bob"); names.stream().map(String::toUpperCase).forEach(System.out::println);
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
What Java 8 added
| Area | Features |
|---|---|
| Language | Lambda expressions, method and constructor references, default and static methods in interfaces, repeating annotations, type annotations, @FunctionalInterface. |
| Library | java.util.function (Function, Predicate, Supplier, Consumer...), java.util.stream, Optional, StringJoiner, String.join, Base64, Objects.requireNonNull helpers. |
| Collections | forEach, removeIf, replaceAll, Map.merge, compute*, getOrDefault, Comparator.comparing, a faster HashMap that treeifies long collision chains. |
| Date and time | java.time: LocalDate, LocalTime, LocalDateTime, ZonedDateTime, Instant, Duration, Period, DateTimeFormatter. |
| Concurrency | CompletableFuture, parallel streams, LongAdder, StampedLock, ForkJoinPool.commonPool(). |
| Platform | The Nashorn JavaScript engine (removed in Java 15), PermGen replaced by Metaspace, jjs and jdeps tools. |
Usage notes
- Lambdas capture only effectively final variables. They are not inner classes:
thisin a lambda is the enclosing instance. - Functional interface means exactly one abstract method; default and static methods do not count.
- Streams are lazy and single-use. Nothing happens until a terminal operation such as
collect,forEachorcount. - Default methods can cause the "diamond" conflict when two interfaces provide the same default; the class must override it, optionally calling
A.super.m(). - Parallel streams use the shared fork-join pool and help only for large, CPU-heavy, side-effect-free work.
- Not in Java 8:
var,List.of,Map.of,Optional.isEmpty,Stream.toList,String.isBlank, records, text blocks. Each arrived later (see 9 to 11 and 12 to 17). - Still running. Many enterprise systems stay on Java 8. Public updates and vendor support for it continue at different levels depending on the vendor.
Optionalas a field or parameter is generally discouraged; it is a return type for "maybe no result".