Java 10 APIs & Features

Java 10 marked the beginning of the new 6-month release cycle, introducing local-variable type inference (var) for more concise and readable code. This release focused on developer productivity with unmodifiable collections, garbage collector improvements, and enhanced container awareness for modern deployment environments.

Released: March 2018
Standard Release

Java 10 Overview

Release Information

Release Date:

March 2018

Support Type:
Standard Release

Quick Summary

Java 10 marked the beginning of the new 6-month release cycle, introducing local-variable type inference (var) for more concise and readable code. This release focused on developer productivity with unmodifiable collections, garbage collector improvements, and enhanced container awareness for modern deployment environments.

Top Features

  • Local-Variable Type Inference (var keyword)
  • Garbage Collector Interface for standardized GC management
  • G1 Improvements with parallel full GC
  • Application Class-Data Sharing for faster startup
  • Unmodifiable Collections with new collectors
  • Container awareness improvements

API Examples

Local-Variable Type Inference (var)

Java 10 introduces the var keyword for local-variable type inference, allowing developers to eliminate explicit type declarations where the type can be inferred from the context. This feature improves code readability and reduces boilerplate while maintaining type safety.

// Traditional approach
List<String> names = Arrays.asList("Alice", "Bob", "Charlie");
Map<String, Integer> nameLength = new HashMap<>();

// Using var (Java 10+)
var names = List.of("Alice", "Bob", "Charlie");
var nameLength = new HashMap<String, Integer>();

// Complex types become simpler
var stream = names.stream()
    .filter(name -> name.startsWith("A"))
    .collect(Collectors.toList());

// Enhanced for-each loops
for (var name : names) {
    System.out.println(name.toUpperCase());
}

Unmodifiable Collections

Java 10 introduces new collectors and copyOf methods for creating unmodifiable collections. These features provide better immutability support, improved performance for read-only collections, and enhanced security by preventing accidental modifications to collection data.

import java.util.stream.Collectors;
import java.util.*;

List<String> names = Arrays.asList("Alice", "Bob", "Charlie");

// Create unmodifiable list
List<String> unmodifiableNames = names.stream()
    .collect(Collectors.toUnmodifiableList());

// Create unmodifiable set
Set<String> unmodifiableSet = names.stream()
    .collect(Collectors.toUnmodifiableSet());

// Create unmodifiable map
Map<String, Integer> unmodifiableMap = names.stream()
    .collect(Collectors.toUnmodifiableMap(
        name -> name,
        String::length
    ));

// List.copyOf() - creates unmodifiable copy
List<String> listCopy = List.copyOf(names);

// Set.copyOf() - creates unmodifiable copy
Set<String> setCopy = Set.copyOf(new HashSet<>(names));

// Map.copyOf() - creates unmodifiable copy
Map<String, Integer> mapCopy = Map.copyOf(Map.of("A", 1, "B", 2));

// Attempting to modify throws UnsupportedOperationException
// unmodifiableNames.add("David"); // Runtime exception

Garbage Collector Interface

Java 10 introduces a standardized Garbage Collector interface that provides better abstraction and management of different garbage collection algorithms. This feature enables easier integration of new garbage collectors and improved monitoring and control of garbage collection behavior.

// Garbage Collector Interface provides better abstraction
// The interface allows for standardized GC management

// Example: Checking available garbage collectors
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;

public class GCInterfaceExample {
    public static void main(String[] args) {
        // Get all garbage collector MXBeans
        for (GarbageCollectorMXBean gcBean : 
             ManagementFactory.getGarbageCollectorMXBeans()) {
            
            System.out.println("GC Name: " + gcBean.getName());
            System.out.println("Collection Count: " + gcBean.getCollectionCount());
            System.out.println("Collection Time: " + gcBean.getCollectionTime() + "ms");
            System.out.println("---");
        }
        
        // Monitor GC activity
        long startTime = System.currentTimeMillis();
        
        // Perform some memory-intensive operations
        for (int i = 0; i < 1000000; i++) {
            new Object();
        }
        
        // Force garbage collection (for demonstration)
        System.gc();
        
        long endTime = System.currentTimeMillis();
        System.out.println("Operation took: " + (endTime - startTime) + "ms");
    }
}

// The GC Interface enables:
// - Better monitoring of GC performance
// - Standardized access to GC metrics
// - Easier integration of new GC algorithms
// - Improved debugging and profiling capabilities