Java 10 Features
1. πΉΒ Local Variable Type Inference (var
)
Description: Allows you to declare local variables using var
, letting the compiler infer the type.
Example:
var name = "Java"; // inferred as String
var count = 10; // inferred as int
var list = List.of("A", "B", "C"); // inferred as List<String>
πΈ Only for local variables (not class members, parameters, or return types).
2. πΉ Unmodifiable Collectors
Improvement in Collectors.toUnmodifiableList()
, Set
, and Map
.
Example:
var list = List.of("A", "B", "C");
var unmodifiableList = list.stream()
.collect(Collectors.toUnmodifiableList());
Trying to modify the list will throw UnsupportedOperationException
.
3. πΉ Optional.orElseThrow() Without Arguments
Description: Simplified way to throw NoSuchElementException
if value is not present.
Example:
Optional<String> name = Optional.of("Java");
System.out.println(name.orElseThrow()); // no args needed
4. πΉ Application Class-Data Sharing (AppCDS)
Benefit: Reduces JVM startup time and memory footprint by sharing common class metadata across JVMs.
Use Case: Large-scale Java applications and microservices.
π Requires specific setup with
-Xshare:on
.
5. πΉ G1 Garbage Collector Improvements
Description: G1 GC now supports full parallel compaction β improving pause times.
6. πΉ Root Certificates Included
Java 10 comes with a default set of root SSL certificates, improving out-of-the-box security for HTTPS connections.
7. πΉ Thread-Local Handshakes
A new JVM mechanism to allow fine-grained pausing of threads for diagnostics and memory management.
π More efficient than global safepoints.
8. πΉ Heap Allocation on Alternative Memory Devices
Supports systems with multi-tiered memory (e.g., DRAM + NVDIMM), allowing Java to allocate heap memory selectively.
Useful for large-scale enterprise deployments.
9. πΉ Experimental Java-Based JIT Compiler (Graal)
You can use Graal as an experimental JIT compiler with the JVM:
java -XX:+UnlockExperimentalVMOptions -XX:+UseJVMCICompiler ...
π Summary Table
Feature | Benefit |
---|---|
var for local variables |
Cleaner syntax, type inference |
Unmodifiable collectors | Immutable collections |
Optional.orElseThrow() |
Simplified null-checks |
AppCDS | Faster startup, reduced memory |
G1 GC enhancements | Shorter pause times |
Root certificates | Secure HTTPS out-of-the-box |
Thread-local handshakes | Better diagnostics, perf |
Heap on alternative memory | Efficient memory usage |
Graal JIT compiler | Performance tuning |