Nearby lessons

114 of 125

Java 9 Features

📌 What You Will Learn
  • JShell — the interactive Java playground
  • Modules (JPMS) — the big architecture change
  • Private methods in interfaces
  • Factory methods for collections: List.of, Set.of, Map.of
  • The updated try-with-resources

Java 9 Features is a core concept of the Java language. This lesson explains Java 9 — The Big Version, Java 8 vs Java 9 — Quick Comparison and Private Methods in Interfaces with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Java 9 — The Big Version

Java 9 (September 2017) brought the largest change to the platform's structure: modules. It also gave us JShell, which made learning Java much more fun, plus many small daily-use improvements.

Java 8 vs Java 9 — Quick Comparison

PointJava 8 (2014)Java 9 (2017)
Headline featureLambda + Stream APIModules (JPMS) + JShell
InterfacesDefault and static methodsAlso private methods
CollectionsArrays.asList(...)List.of / Set.of / Map.of (immutable)
try-with-resourcesVariables inside try bracketsExisting variables allowed
Learning toolCommand line onlyJShell interactive playground

Private Methods in Interfaces

Java 8 gave interfaces default and static methods. Java 9 lets interfaces also have private methods — hidden helper methods that default methods can share inside the interface.

Example03
JCode Cell
1interface Demo {
2 default void show() {
3 helper();
4 }
5 private void helper() { // private - hidden inside interface
6 System.out.println("Shared helper logic");
7 }
8}

Try-with-Resources Improvement

In Java 7, the resource variable had to be declared inside the try brackets. Java 9 lets us use variables already declared outside:

Example04
JCode Cell
1BufferedReader br = new BufferedReader(new FileReader("a.txt"));
2 
3try (br) { // Java 9: use an existing variable
4 System.out.println(br.readLine());
5} // br closes automatically

More Small Java 9 Additions

  • String.chars() — a stream of the characters of a String.
  • Optional.stream() — turns Optional into a stream.
  • Stream.ofNullable() — safe stream of a possibly-null value.
  • Stream.iterate(...) enhancements for creating streams.
  • List.of / Map.of can hold at most 10 pairs — beyond that use Map.ofEntries(...).
📝 Key Takeaways
  • JShell lets you test Java interactively without files.
  • JPMS modules group packages with clear exports and requirements.
  • Interfaces can now have private helper methods.
  • List.of / Set.of / Map.of create short, immutable collections.
  • try-with-resources can use existing variables (Java 9).
  • New stream helpers: Optional.stream(), Stream.ofNullable().

🧠 Test Your Knowledge

2 Questions
Progress: 0 / 2