Nearby lessons

115 of 125

Java 10 Features

📌 What You Will Learn
  • var — local variable type inference
  • Copy factory methods: List.copyOf, Set.copyOf, Map.copyOf
  • Optional.orElseThrow
  • Other small improvements

Java 10 Features is a core concept of the Java language. This lesson explains Java 10 — The Release Schedule Change, var — Let Java Guess the Type and var vs Explicit Type with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Java 10 — The Release Schedule Change

Java 10 (March 2018) is a small version, but it marks a big change: from Java 10 onwards, a new Java version arrives every six months (in March and September). The star feature of Java 10 is var.

var — Let Java Guess the Type

var lets the compiler figure out the type from the value on the right. You write var instead of the long type name, and Java infers it.

Trainer's Note: Important rules of var: 1) var is for local variables only — you cannot use it for fields or method parameters. 2) You must give a value on the same line (var x; alone is an error). 3) var cannot be null (Java cannot guess null's type). 4) It is a reserved type name, not a keyword — old code using var as a name still works.
Example02
JCode Cell
1// Old way - type written clearly
2String name = "Rahul";
3ArrayList<String> list = new ArrayList<>();
4 
5// New way with var - Java guesses the type
6var name = "Rahul"; // inferred as String
7var list = new ArrayList<String>(); // inferred as ArrayList<String>
8var count = 10; // inferred as int
9var price = 99.5; // inferred as double

var — Let Java Guess the Type

In simple words: `var` only replaces the type name — Java still infers a fixed type from the right-hand value. The variable stays strongly typed, so var a = 100; remains an int and a = "text" will not compile.
Example03
JCode Cell
1var s = "Hello"; // OK - String
2s.toUpperCase(); // works
3 
4var x; // ERROR! must initialize
5var n = null; // ERROR! cannot infer null
6 
7// var does NOT change Java's strong typing
8var a = 100;
9// a = "text"; // ERROR! a is still int
10 
11// var makes long generic types beautiful
12var students = new ArrayList<Student>();
13var map = new HashMap<String, List<Integer>>();

var vs Explicit Type

PointExplicit type (old)var (Java 10)
ExampleArrayList<String> list = new ArrayList<>();var list = new ArrayList<String>();
Who decides the typeYou write it yourselfJava infers it from the right side
ReadabilityType visible, code longerShorter, type less obvious
Type safetyStrongSame — still strongly typed
Where allowedLocal variables, fields, parametersLocal variables only

Copy Methods — Safe Copies of Collections

Java 10 adds copyOf methods that make an immutable copy of a collection:

In simple words: `copyOf` makes a separate immutable copy of the collection — changing the original later never touches the copy, and the copy itself refuses any add, remove or set.
Example05
JCode Cell
1import java.util.*;
2 
3class CopyDemo {
4 public static void main(String[] args) {
5 var original = new ArrayList<>(List.of("A", "B", "C"));
6 
7 var copy = List.copyOf(original); // immutable copy
8 System.out.println(copy);
9 
10 // original can still change, copy cannot
11 original.add("D");
12 System.out.println("Original: " + original);
13 System.out.println("Copy: " + copy);
14 }
15}
Output
[A, B, C] Original: [A, B, C, D] Copy: [A, B, C]

Optional.orElseThrow()

Java 10 gives Optional a cleaner way to throw an exception if empty:

In simple words: `orElseThrow()` means "give me the value, or throw an exception if the Optional is empty" — it is the shortest way to write a value-or-fail check in one line.
Example06
JCode Cell
1import java.util.*;
2 
3class OptionalDemo10 {
4 public static void main(String[] args) {
5 Optional<String> box = Optional.empty();
6 
7 // orElseThrow: if empty, throws NoSuchElementException
8 // if present, returns the value
9 String v = box.orElseThrow();
10 System.out.println(v);
11 }
12}
Output
Exception in thread "main" java.util.NoSuchElementException: No value present

Other Small Java 10 Improvements

  • Application Class Data Sharing — faster startup by sharing class data.
  • Garbage Collector interface — clean way to swap GC implementations.
  • Enhanced `@Deprecated` — you can say why something is deprecated.
  • Container awareness — the JVM now correctly reads CPU and memory limits of containers (Docker).
  • Unmodifiable `toUnmodifiableList()` collectors added later in Java 10 too.

For a beginner, the most useful thing in Java 10 is var — it makes code shorter and cleaner. The rest are mostly for advanced system-level users.

📝 Key Takeaways
  • From Java 10, a new version arrives every six months.
  • var lets Java infer the type of a local variable — but only for locals, with initialization.
  • List.copyOf / Set.copyOf / Map.copyOf create immutable copies.
  • Optional.orElseThrow() throws if empty, or returns the value.
  • var keeps Java strongly typed — the type is just inferred, not removed.

🧠 Test Your Knowledge

8 Questions
Progress: 0 / 8