Java 12 APIs & Features

Java 12 introduces significant language improvements with Switch Expressions as a preview feature, enhanced string manipulation capabilities, and advanced garbage collection optimizations. This release focuses on developer productivity, performance improvements, and modern language constructs for better code readability and maintainability.

Released: March 2019
Standard Release

Java 12 Overview

Release Information

Release Date:

March 2019

Support Type:
Standard Release

Quick Summary

Java 12 introduces significant language improvements with Switch Expressions as a preview feature, enhanced string manipulation capabilities, and advanced garbage collection optimizations. This release focuses on developer productivity, performance improvements, and modern language constructs for better code readability and maintainability.

Top Features

  • Switch Expressions (Preview) - More concise and expressive conditional logic
  • String Improvements - Enhanced string manipulation with indent() and transform()
  • G1 Garbage Collector improvements - Better performance and pause time reduction
  • Shenandoah Experimental Garbage Collector - Low-latency garbage collection
  • JVM Constants API - Better constant handling and reflection capabilities
  • Default CDS Archives - Faster application startup times

API Examples

Switch Expressions (Preview)

Java 12 introduces Switch Expressions as a preview feature, providing a more concise and expressive way to write conditional logic. This feature eliminates the need for break statements, reduces boilerplate code, and enables switch statements to be used as expressions, making code more readable and maintainable.

// Traditional switch statement
String dayType;
switch (dayOfWeek) {
    case MONDAY:
    case TUESDAY:
    case WEDNESDAY:
    case THURSDAY:
    case FRIDAY:
        dayType = "Weekday";
        break;
    case SATURDAY:
    case SUNDAY:
        dayType = "Weekend";
        break;
    default:
        dayType = "Unknown";
}

// Switch expression (Java 12 preview)
String dayType = switch (dayOfWeek) {
    case MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY -> "Weekday";
    case SATURDAY, SUNDAY -> "Weekend";
    default -> "Unknown";
};

// With yield for complex logic
int numLetters = switch (dayOfWeek) {
    case MONDAY, FRIDAY, SUNDAY -> 6;
    case TUESDAY -> 7;
    case THURSDAY, SATURDAY -> 8;
    case WEDNESDAY -> 9;
    default -> {
        System.out.println("Unknown day");
        yield 0;
    }
};

String Improvements

Java 12 enhances string manipulation capabilities with new methods like indent() and transform(). These additions provide more flexible string formatting, improved readability for multi-line text, and functional programming approaches to string transformation, making string processing more efficient and expressive.

// String.indent() - adds indentation
String text = "Hello\nWorld";
String indented = text.indent(4);

// String.transform() - applies function to string
String result = "hello".transform(String::toUpperCase);

// More complex string transformations
String json = """
    {
        "name": "John",
        "age": 30
    }
    """.indent(4);

// Chaining transformations
String processed = "hello world"
    .transform(String::toUpperCase)
    .transform(s -> s.replace(" ", "_"))
    .transform(s -> "processed: " + s);

G1 Garbage Collector Improvements

Java 12 introduces significant improvements to the G1 Garbage Collector, including abortable mixed collections and better pause time management. These enhancements provide more predictable garbage collection behavior, reduced pause times, and improved overall application performance for memory-intensive workloads.

// G1 GC improvements in Java 12
// Enable G1 GC with improved settings
// -XX:+UseG1GC -XX:MaxGCPauseMillis=200

// Abortable mixed collections
// G1 can now abort mixed collections if they take too long
// This prevents long pause times

// Better pause time management
// G1 now provides more predictable pause times
// Improved concurrent marking and evacuation

// Example: Monitoring G1 performance
import java.lang.management.GarbageCollectorMXBean;
import java.lang.management.ManagementFactory;

public class G1Monitor {
    public static void printG1Stats() {
        for (GarbageCollectorMXBean gcBean : 
             ManagementFactory.getGarbageCollectorMXBeans()) {
            if (gcBean.getName().contains("G1")) {
                System.out.println("G1 GC: " + gcBean.getName());
                System.out.println("Collection count: " + gcBean.getCollectionCount());
                System.out.println("Collection time: " + gcBean.getCollectionTime() + "ms");
            }
        }
    }
}

// Benefits of G1 improvements:
// - Reduced pause times
// - More predictable performance
// - Better handling of large heaps
// - Improved concurrent processing