Java 9 to 11 Features

Naveen AnamaneniUpdated · 5 min read

After Java 8, the next release took three and a half years. That was too slow, so with Java 9 the Java team switched to a new, predictable rhythm: a new release every six months, and a long-term support (LTS) release every few years. Java 9, 10 and 11 were the first three releases of that new rhythm, and Java 11 became the next LTS after Java 8. In this lesson, you will get a quick reference to what each of the three added, with a tour program and the lessons that cover each feature.

Java 9 (September 2017)

FeatureWhat it gives youRead more
Module system (JPMS)module-info.java declares what a module requires and exports"Java Platform Module System (JPMS)"
JShellAn interactive prompt to try Java code line by line"JShell (Interactive Java)"
Collection factory methodsList.of, Set.of, Map.of for small immutable collections"Immutable Collections in Java"
Private interface methodsShared helper code inside interfaces"Default, Static and Private Methods in Interfaces"
Stream additionstakeWhile, dropWhile, ofNullable, iterate with a condition"Creating Streams in Java"
Optional additionsifPresentOrElse, or, stream"Optional Class in Java"
Try-with-resources on existing variablestry (in) { ... } when in is effectively final"Try-With-Resources in Java"
jlinkBuild a small custom runtime"Jlink in Java: Custom Runtime Images"
G1 becomes the default garbage collectorShorter pauses by default
Compact stringsLatin-1 strings use 1 byte per character internally

Java 10 (March 2018)

FeatureWhat it gives youRead more
var for local variablesvar list = new ArrayList<String>();"Local Variable Type Inference"
Unmodifiable copiesList.copyOf, Collectors.toUnmodifiableList"Immutable Collections in Java"
Optional.orElseThrow()A clearer name for get()"Optional Class in Java"
Time-based version numbersVersions named by release, every six months"Java Versions and Release Cycle"

Java 11 (September 2018, LTS)

FeatureWhat it gives youRead more
Standard HttpClientModern HTTP/1.1 and HTTP/2 client, sync and async"HttpClient in Java"
New String methodsisBlank, strip, lines, repeat"Modern String Methods in Java"
Files.readString and writeStringRead or write a whole text file in one call"NIO.2 Path and Files Classes"
Run a single source filejava Hello.java without javac"Running a Single Source File Directly"
var in lambda parameters(var a, var b) -> a + b, so annotations can be added"Local Variable Type Inference"
Predicate.notfilter(Predicate.not(String::isBlank))"Built-In Functional Interfaces in Java"
Java EE and CORBA modules removedJAXB, JAX-WS and friends now come from separate libraries
Flight Recorder open-sourced, TLS 1.3Free low-overhead profiling; modern secure connections

A Java 9 to 11 Tour

Let's see a program that uses features from all three releases.

Tour11.java▶ Try it
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.stream.Stream;
 
public class Tour11 {
    public static void main(String[] args) throws Exception {
        var names = List.of("Navika", "  ", "Srihitha", "Advitha"); // 9, 10
        var marks = Map.of("Navika", 91, "Advitha", 77);            // 9
 
        names.stream()
                .filter(Predicate.not(String::isBlank))             // 11
                .forEach(n -> System.out.println("student: " + n));
 
        Stream.of(40, 55, 72, 30, 90)
                .takeWhile(m -> m >= 35)                            // 9
                .forEach(m -> System.out.println("pass streak: " + m));
 
        Optional.ofNullable(marks.get("Srihitha"))
                .ifPresentOrElse(                                   // 9
                        m -> System.out.println("marks: " + m),
                        () -> System.out.println("Srihitha: not entered"));
 
        System.out.println("[" + "  padded  ".strip() + "]");       // 11
        System.out.println("=".repeat(10));                         // 11
 
        Path file = Files.createTempFile("note", ".txt");
        Files.writeString(file, "line one\nline two\n");            // 11
        System.out.println(Files.readString(file).lines().count()
                + " lines");                                        // 11
        Files.delete(file);
    }
}
Output
student: Navika
student: Srihitha
student: Advitha
pass streak: 40
pass streak: 55
pass streak: 72
Srihitha: not entered
[padded]
==========
2 lines

Explanation:

  1. List.of and Map.of create small immutable collections; var lets the compiler infer their types.
  2. Predicate.not(String::isBlank) keeps only non-blank names.
  3. takeWhile stops at the first mark below 35, unlike filter, which would check every element.
  4. ifPresentOrElse handles both cases of an Optional in one call.
  5. strip, repeat, writeString, readString and lines replace several lines of older code each.

Practice

  1. Easy: In JShell, try " Navika ".strip(), "".isBlank(), "ab".repeat(3) and List.of(1, 2).add(3). Explain each result.
  2. Harder: Write a single-file program Count.java that reads a text file named in args[0] with Files.readString and prints how many non-blank lines it has. Run it with java Count.java notes.txt, without javac.

Quick check: Java 9 to 11

4 questions
1Which release introduced var for local variables?
2Which release was the LTS after Java 8?
3What does List.of(...) return?
4Which Java 11 feature lets you run java Hello.java directly?

Key Takeaways

Was this tutorial helpful?

Lesson 562 of 578