Java 9 to 11 Features
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)
| Feature | What it gives you | Read more |
|---|---|---|
| Module system (JPMS) | module-info.java declares what a module requires and exports | "Java Platform Module System (JPMS)" |
| JShell | An interactive prompt to try Java code line by line | "JShell (Interactive Java)" |
| Collection factory methods | List.of, Set.of, Map.of for small immutable collections | "Immutable Collections in Java" |
| Private interface methods | Shared helper code inside interfaces | "Default, Static and Private Methods in Interfaces" |
| Stream additions | takeWhile, dropWhile, ofNullable, iterate with a condition | "Creating Streams in Java" |
Optional additions | ifPresentOrElse, or, stream | "Optional Class in Java" |
| Try-with-resources on existing variables | try (in) { ... } when in is effectively final | "Try-With-Resources in Java" |
jlink | Build a small custom runtime | "Jlink in Java: Custom Runtime Images" |
| G1 becomes the default garbage collector | Shorter pauses by default | |
| Compact strings | Latin-1 strings use 1 byte per character internally |
Java 10 (March 2018)
| Feature | What it gives you | Read more |
|---|---|---|
var for local variables | var list = new ArrayList<String>(); | "Local Variable Type Inference" |
| Unmodifiable copies | List.copyOf, Collectors.toUnmodifiableList | "Immutable Collections in Java" |
Optional.orElseThrow() | A clearer name for get() | "Optional Class in Java" |
| Time-based version numbers | Versions named by release, every six months | "Java Versions and Release Cycle" |
Java 11 (September 2018, LTS)
| Feature | What it gives you | Read more |
|---|---|---|
Standard HttpClient | Modern HTTP/1.1 and HTTP/2 client, sync and async | "HttpClient in Java" |
New String methods | isBlank, strip, lines, repeat | "Modern String Methods in Java" |
Files.readString and writeString | Read or write a whole text file in one call | "NIO.2 Path and Files Classes" |
| Run a single source file | java 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.not | filter(Predicate.not(String::isBlank)) | "Built-In Functional Interfaces in Java" |
| Java EE and CORBA modules removed | JAXB, JAX-WS and friends now come from separate libraries | |
| Flight Recorder open-sourced, TLS 1.3 | Free 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 linesExplanation:
List.ofandMap.ofcreate small immutable collections;varlets the compiler infer their types.Predicate.not(String::isBlank)keeps only non-blank names.takeWhilestops at the first mark below 35, unlikefilter, which would check every element.ifPresentOrElsehandles both cases of anOptionalin one call.strip,repeat,writeString,readStringandlinesreplace several lines of older code each.
Practice
- Easy: In JShell, try
" Navika ".strip(),"".isBlank(),"ab".repeat(3)andList.of(1, 2).add(3). Explain each result. - Harder: Write a single-file program
Count.javathat reads a text file named inargs[0]withFiles.readStringand prints how many non-blank lines it has. Run it withjava Count.java notes.txt, withoutjavac.
Key Takeaways
Was this tutorial helpful?
Lesson 562 of 578