
Java 12, released in March 2019, introduced several new features and improvements to the Java programming language. These updates aim to enhance developer productivity, improve code readability, and optimize performance. In this blog, we will delve into the major features of Java 12, along with code examples to help you understand their practical applications.
Switch Expressions (Preview)
Java 12 introduced switch expressions as a preview feature, which enhances the traditional switch statement. Switch expressions allow for more concise and readable code.
Example:
public class SwitchExpressionsExample {
public static void main(String[] args) {
String day = "MONDAY";
int numLetters = switch (day) {
case "MONDAY", "FRIDAY", "SUNDAY" -> 6;
case "TUESDAY" -> 7;
case "THURSDAY", "SATURDAY" -> 8;
case "WEDNESDAY" -> 9;
default -> throw new IllegalStateException("Unexpected value: " + day);
};
System.out.println("Number of letters in " + day + ": " + numLetters);
}
}
Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
Shenandoah is an experimental garbage collector that aims to reduce pause times by performing evacuation work concurrently with the running Java threads.
How to Enable Shenandoah:
java -XX:+UnlockExperimentalVMOptions -XX:+UseShenandoahGC -jar your-application.jar
Microbenchmark Suite
Java 12 includes a basic suite of microbenchmarks based on the Java Microbenchmark Harness (JMH). This suite helps developers measure the performance of their code.
Example Benchmark:
import org.openjdk.jmh.annotations.Benchmark;
public class MyBenchmark {
@Benchmark
public void testMethod() {
// Your benchmark code here
}
}
Compact Number Formatting
Java 12 introduced compact number formatting to format large numbers in a compact style based on locale-specific rules. This feature is useful for displaying numbers in a more readable format.
NumberFormat nf = NumberFormat.getCompactNumberInstance(Locale.US, Style.SHORT);
String formatted = nf.format(1000);
System.out.println(formatted); // Output: 1K
Improvements in String
Java 12 improved the String class with more efficient handling of Strings, including new methods such as indent, which allows easy indentation of multi-line strings.
String text = "Hello\nWorld";
String indented = text.indent(2); // Indent each line by 2 spaces
System.out.println(indented);
JVM Constants API
The JVM Constants API provides a way to model key class-file and run-time artifacts, especially those that describe constants that are loadable from the constant pool.
Example:
import java.lang.constant.ClassDesc;
import java.lang.constant.MethodTypeDesc;
public class JVMConstantsExample {
public static void main(String[] args) {
ClassDesc stringClass = ClassDesc.of("java.lang.String");
MethodTypeDesc methodType = MethodTypeDesc.of(void.class, String.class);
System.out.println("ClassDesc: " + stringClass);
System.out.println("MethodTypeDesc: " + methodType);
}
}
Default CDS Archives
Java 12 introduced default Class Data Sharing (CDS) archives to improve startup time by sharing common class metadata across JVM instances, reducing memory footprint and improving performance.
How to Enable:
This feature is enabled by default, but you can customize it using the following options:
java -XX:ArchiveClassesAtExit=hello.jsa -cp hello.jar Hello
Deprecated and Removed Features
Java 12 also marked several features as deprecated or removed, ensuring cleaner and more maintainable code bases going forward.
- Deprecated APIs: Continued removal and deprecation of outdated APIs.
- JVM Constants API: An incubating API to model nominal descriptions of key class-file and run-time artifacts.
Conclusion
Java 12 introduced significant improvements and new features aimed at enhancing developer productivity, performance, and the overall Java ecosystem. Whether you’re leveraging switch expressions for cleaner code or exploring new garbage collection strategies, Java 12 provides tools to build faster, more efficient applications.
Reference
https://www.oracle.com/java/technologies/javase/12-relnote-issues.html
For further insights, please Check my profile.
