Java 9 Features

Java 9 was one of the biggest releases since Java 8.
Instead of “just” adding APIs, Java 9 changed how we structure applications, how we write code, and how we work interactively.

In this guide, we’ll walk through the most important Java 9 features — simply, clearly, and with small snippets.

1. Java Platform Module System (JPMS) — Project Jigsaw

Before Java 9, everything was packaged into one big rt.jar.

With modules, Java gained:

  • Better encapsulation
  • Faster startup
  • Smaller deployable images
  • Explicit dependencies

📌 How modules look

Add a module-info.java file in your project:

module com.example.app { requires java.sql; exports com.example.app.core; }


2. JShell — REPL for Java

Interactive tool for quickly testing code without creating classes or projects.

jshell> int x = 5
jshell> x * 2
$2 ==> 10

Great for:

  • experimenting
  • demos
  • learning Java faster

3. Collection Factory Methods

Easier way to create small immutable collections.

var list = List.of("A", "B", "C");
var set  = Set.of(1, 2, 3);
var map  = Map.of("id", 1, "name", "Susil");

Before Java 9 → lots of boilerplate.


4. Stream API Enhancements

New methods like takeWhile, dropWhile, and iterate overload.

Stream.of(1,2,3,4,5,0,6)
      .takeWhile(n -> n < 5)
      .forEach(System.out::println);
// Output: 1 2 3 4

5. Optional Improvements

Optional got new helper methods like ifPresentOrElse.

Optional<String> name = Optional.empty();

name.ifPresentOrElse(
    System.out::println,
    () -> System.out.println("No value present")
);

6. Private Interface Methods

Interfaces can have reusable private helper methods.

interface Logger {
    default void info(String msg) {
        log("INFO", msg);
    }

    private void log(String level, String msg) {
        System.out.println(level + ": " + msg);
    }
}

7. New HTTP/2 Client (Incubator)

Modern HTTP client replacing old HttpURLConnection.

var client = HttpClient.newHttpClient();
var request = HttpRequest.newBuilder(URI.create("https://example.com")).build();

var response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());

Supports:

  • HTTP/2
  • async calls
  • WebSocket

8 Enhanced try-with-resources

You can reuse an already-declared resource.

BufferedReader br = new BufferedReader(new FileReader("data.txt"));
try (br) {
    System.out.println(br.readLine());
}

Before Java 9 → had to declare inside try.


9 Process API Updates

Easier to work with OS processes.

ProcessHandle.current()
    .info()
    .command()
    .ifPresent(System.out::println);

10. Multi-Release JARs

Ship different class versions in the same JAR for different Java versions.

Structure example:

/META-INF/versions/9/...

Allows backward compatibility while using newer features.


11. Reactive Streams (Flow API)

Introduced java.util.concurrent.Flow (Publisher/Subscriber model).

Flow.Publisher<Integer> publisher;
Flow.Subscriber<Integer> subscriber;

Foundation for reactive frameworks.


12. G1 GC as Default

Garbage First (G1) became the default garbage collector — better pause times and performance.

No code — runtime improvement.


✔️ Summary

Java 9 focused on:

  • Modularity
  • Developer productivity (JShell, collection factories)
  • Performance & tooling
  • APIs for modern apps (HTTP/2, reactive)