What is new in Java 12 (for Developers)
Java JDK 12 was released on March 19 as part of the new release cicle (a new Java version every 6 months). In this article we will learn what are the new features available for developers.
What’s new in Java 12?
For Java JDK 12, there are not many new API features. Most enhancements are internal Java/JVM improvements, but there is one new Java feature (JEP) for us, developers.
Below is a list of all features included in Java 12 (from Oracle blog):
- JEP 325 – Switch Expressions (preview): Extends the switch statement so that it can be used as either a statement or an expression, and that both forms can use either a “traditional” or “simplified” scoping and control flow behavior. These changes will simplify everyday coding, and also prepare the way for the use of pattern matching (JEP 305) in switch. For JDK 12 this feature is available as a preview language feature (JEP 12).
- JEP 344 – Abortable Mixed Collections for G1: Makes G1 mixed collections abortable if they might exceed the pause target.
- JEP 346 – Promptly Return Unused Committed Memory from G1: Enhances the G1 garbage collector to automatically return Java heap memory to the operating system when idle.
- JEP 189 – Shenandoah, A Low-Pause-Time Garbage Collector (experimental): Adds a new garbage collection (GC) algorithm which reduces GC pause times by doing evacuation work concurrently with the running Java threads.
- JEP 230 – Microbenchmark Suite: Adds a basic suite of microbenchmarks to the JDK source code, and makes it easy for developers to run existing microbenchmarks and create new ones.
- JEP 334 – JVM Constants API: Introduces an API to model nominal descriptions of key class-file and run-time artifacts, in particular constants that are loadable from the constant pool.
- JEP 340 – One AArch64 Port, Not Two: Removes all of the sources related to the arm64 port while retaining the 32-bit ARM port and the 64-bit aarch64 port.
- JEP 341 – Default CDS Archives: Enhance the JDK build process to generate a class data-sharing (CDS) archive, using the default class list, on 64-bit platforms.
Also JEP 326 – Raw String Literals was withdrawn from JDK 12. However, there are 2 new String methods from this JEP that were included in JDK 12.
Let’s take a look at Switch Expressions and the new String methods.
Before you try these new features, don’t forget to download and install the latest JDK. Also, don’t forget to download the latest version of your favorite IDE (Eclipse IDE 2019‑03 or IntelliJ IDEA 2019.1).
New String methods (from JEP 326): indent and transform
There are two new String methods available in JDK 12: indent
and transform
. Both methods are from JEP 326 – Raw String Literals that was withdrawn from JDK 12.
First method is indent
, which as the name says, will apply indentation (number of spaces) as demonstrated below:
var result = "Hello\nWorld!".indent(3);
System.out.println(result);
Output will be:
Hello
World!
Second method is transform
, which receives a function/lambda with a String as paramater and returns an object.
var result = "Hello".transform(s -> s + ", World!");
System.out.println(result); // Hello, World!
This method can also be chained. This is very useful and easier to read in case we need to transform/manipulate a string more than once:
var result = "hello"
.transform(s -> s + ", world!")
.transform(String::toUpperCase);
System.out.println(result); // HELLO, WORLD!
Switch Expressions (from JEP 325 - Preview Feature)
Two new features were added to switch expressions:
- Introduction of
case value ->
syntax that removes the need ofbreak
statements. - Now we can assign a Switch expression to a variable to return it as value of a method.
You can see these new features in the code below.
Let’s start by writing a traditional switch statement:
private String traditionalSwitch(DAY_OF_WEEK dayOfWeek) {
String result;
switch (dayOfWeek) {
case MONDAY:
case TUESDAY:
case WEDNESDAY:
case THRUSDAY:
case FRIDAY:
result = "Weekday";
break;
case SATURDAY:
case SUNDAY:
result = "Weekend";
break;
default:
result = "Invalid day!";
}
return result;
}
Now let’s refactor it so we can use the case value ->
syntax:
private String newSwitchFeature1(DAY_OF_WEEK dayOfWeek) {
String result;
switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY -> result = "Weekday";
case SATURDAY, SUNDAY -> result = "Weekend";
default -> result = "Invalid day!";
}
return result;
}
Looks much better and easier to read. We can also have a body using the case value ->
syntax:
case SATURDAY, SUNDAY -> {
result = "Weekend";
System.out.println("Weekend");
}
Inside each case
statement we are assigning a value to a variable and returning it at the end of the method. We can simplify this code a little bit more:
private String newSwitchFeature2(DAY_OF_WEEK dayOfWeek) {
var result = switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY -> "Weekday";
case SATURDAY, SUNDAY -> "Weekend";
};
return result;
}
And if we don’t want to assign the switch to a variable, we can also return it directly:
private String newSwitchFeature2(DAY_OF_WEEK dayOfWeek) {
return switch (dayOfWeek) {
case MONDAY, TUESDAY, WEDNESDAY, THRUSDAY, FRIDAY -> "Weekday";
case SATURDAY, SUNDAY -> "Weekend";
};
}
Compared to the traditional switch, this is a nice refactoring!
As this is a preview feature, you’ll need to enable preview features in your favorite IDE as well in order to be able to compile and run the code:
Conclusion
These are very two simple features, which makes easier for us, developers, to keep ourselves up to date with latest Java features. Now let’s wait for Java 13 (September 2019) with more new features!
View the full source code on GitHub.
References:
Happy Coding!