Nearby lessons
115 of 125Java 10 Features
- 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.
var — Let Java Guess the Type
var vs Explicit Type
| Point | Explicit type (old) | var (Java 10) |
|---|---|---|
| Example | ArrayList<String> list = new ArrayList<>(); | var list = new ArrayList<String>(); |
| Who decides the type | You write it yourself | Java infers it from the right side |
| Readability | Type visible, code longer | Shorter, type less obvious |
| Type safety | Strong | Same — still strongly typed |
| Where allowed | Local variables, fields, parameters | Local variables only |
Copy Methods — Safe Copies of Collections
Java 10 adds copyOf methods that make an immutable copy of a collection:
Optional.orElseThrow()
Java 10 gives Optional a cleaner way to throw an exception if empty:
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.
- 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.