What’s New in Java 10? Exploring var, Immutable Collections, and Optional Enhancements

This article reviews the key Java 10 features—including the shortened six‑month release cycle, local‑variable type inference with the new var keyword, strengthened immutable collection APIs, the Optional.orElseThrow() method, and other performance and container‑integration improvements—while highlighting best‑practice pitfalls.

Programmer DD
Programmer DD
Programmer DD
What’s New in Java 10? Exploring var, Immutable Collections, and Optional Enhancements

Java 10

Starting with Java 10 the release cycle was shortened to six months, delivering a new version twice a year.

Local Variable Type Inference

In earlier Java versions you had to declare generic types explicitly, e.g.:

Map<String, String> map = new HashMap<String, String>();

Java 7 introduced the diamond operator, simplifying the declaration to: Map<String, String> map = new HashMap<>(); Java 10 further elevates type inference with the var keyword:

var map = Map.of("hello", "world");
String var = map.get("hello");

The compiler infers the variable type from the right‑hand initializer, dramatically reducing boiler‑plate code. This feature applies only to local variables; it cannot be used for fields, method parameters, or return types.

The var identifier is not a Java keyword, preserving backward compatibility. Using var incurs no runtime overhead and the variable’s type is still determined at compile time.

Avoid Overusing var

While var can make code concise, misuse can hurt readability, especially when the inferred type is not obvious: var data = someObject.getData(); In stream pipelines, excessive var usage may obscure the data flow:

// Poor readability
var names = apples.stream()
    .map(Apple::getName)
    .collect(Collectors.toList());

When polymorphism is involved, var can hide type mismatches; the inferred type becomes fixed after the first assignment, leading to compile‑time errors if reassigned to an incompatible type.

Immutable Collections

Java 10 strengthens immutable collection support, a cornerstone of functional programming and thread‑safe code. New APIs include collection copying and stream collectors that produce immutable results.

Collection Copy

Creating an immutable copy of a list: List<Apple> copyList = List.copyOf(apples); Any attempt to modify such a collection throws java.lang.UnsupportedOperationException.

Stream to Immutable Collection

Previously collect(Collector) produced mutable collections. Java 10 adds immutable collectors, for example:

List<String> names = apples.stream()
    .map(Apple::getName)
    .collect(Collectors.toUnmodifiableList());

Optional.orElseThrow()

Optional<String> optional = Optional.ofNullable(nullableVal);
String value = optional.get(); // May throw NoSuchElementException

Calling get() on an empty Optional is ambiguous. Java 10 introduces orElseThrow() to make the intent explicit and improve semantics.

Other Enhancements

Java 10 brings performance gains, including parallel G1 garbage collection and a new JIT compiler that speeds up execution. Container integration is improved: the JVM now detects container‑provided CPU and memory limits and adjusts its resource usage accordingly. Additional low‑level optimizations are present but omitted here for brevity.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

javaoptionalType InferenceImmutable CollectionsVarJava 10
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.