Java 10 Features with detailed example
Java 10 was released on March 10, 2018. With Java 10, Oracle has changed the java release cadence to a new model, a 6 month release cadence and Long Term Support model for Oracle Java SE products.
Few major features in Java 10
1. Optional class Enhancement
orElseThrow(Supplier<? extends X> exceptionSupplier): Returns the value if present; otherwise, throws the exception provided by the supplier.
var optionalString = Optional.ofNullable("Optional");
var result = optionalString.orElseThrow(); //similar to get if value not null
System.out.println(result); //print Optional
String nullString = null;
var optionalString = Optional.ofNullable(nullString);
var result = optionalString.orElseThrow();
System.out.println(result); //throw exception2. Local-variable type inference
Java 10 introduced the ability to use the var keyword for local variable type inference. This allows you to declare local variables without explicitly specifying the type, relying instead on the compiler to infer it from the initialization expression.
var list = new ArrayList<String>();3. Immutable Collections:
In Java 10, new methods are introduced to get the immutable collection. In List, Set and Map interfaces, a new method copyOf() has been introduced. That can create new collection instances from existing ones.
New methods in Collector class to get elements of a stream into an unmodifiable collection.
1. List.copyOf(), Set.copyOf(), and Map.copyOf()
Get Priya’s stories in your inbox
Join Medium for free to get updates from this writer.
2. Collectors.toUnmodifiableList(), toUnmodifiableSet(), and toUnmodifiableMap()
public static void main(String[] args)
{
var immutableList = List.of("Harry Potter", "Hermoinie", "Ron Weasely", "Dumbledore", "Sirius Black", "Nagini"); try
try{
// Get an unmodifiable list by use of copyOf()
List<String> copyList = List.copyOf(immutableList);
// Try to add element in unmodifiable list
copyList.add("Sanpe");
}
catch(UnsupportedOperationException e)
{
System.out.println("Collection can't modify.");
}
try
{
// Get an unmodifiable list by use Collector
List<String> copyList = immutableList.stream()
.collect(Collectors.toUnmodifiableList());
copyList.add("Snake");
}
catch(UnsupportedOperationException e)
{
System.out.println("Collection can't modify.");
}
}
//similar to List.copyOf()-> Set.copyOf(), Map.copyOf() are immutable4. Parallel Full GC in Java 10
In Java 10, the G1 garbage collector was enhanced to include a parallel full garbage collection (Full GC) feature. Prior to Java 10, G1 performed full garbage collections using a single-threaded approach, which could lead to longer pause times when large amounts of garbage needed to be collected.
The introduction of parallel Full GC in Java 10 aimed to address this limitation by allowing multiple threads to be used for the Full GC phase. This enhancement can significantly reduce the duration of Full GC pauses in G1, especially in scenarios where there is a large heap or when there are many garbage-collectable objects.
Multi-threaded Approach: Parallel Full GC in G1 allows multiple threads to work concurrently to perform the Full GC. This parallelism helps in reducing pause times because the work is distributed among multiple threads, allowing for faster garbage collection.
- Improved Responsiveness
- Configuration and Tuning
5. Experimental Java-Based JIT Compiler (JEP 317)
Java 10 included an experimental Java-based Just-In-Time (JIT) compiler, called Graal, which was developed as part of Project Graal. This compiler is designed to improve the performance of Java applications, particularly in the context of long-running processes.
6. Time-Based Release Versioning:
From Java 10 onwards, Oracle releases a new version of Java after every 6 month where each version contains one or two major features. Oracle uses a release train concept. Each release train is scheduled for 6 months. Features which are developed within this timeline are shipped in the release otherwise the features are moved to next release train.
New API’s have been added to get these version values programmatically.
//from code
Version version = Runtime.version();
version.feature();
version.interim();
version.update();
version.patch();
//from shell
$ java -version
java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)7. Other Enhancements:
- Improved container awareness: JDK 10 introduced improvements to make Java more aware of being run in a containerized environment, including better resource management.
- Consolidation of the JDK Forest into a single repository: The JDK source code was restructured to be stored in a single repository, simplifying development and making it easier to contribute to OpenJDK.






