Finally, JDK 12 which is a part of the six-month release cycle, is here. It comes after the last Java LTS version 11. We had discussed at length on Java 11 features before. Today we’ll be discussing Java 12 features and see what it has in store for developers. Java 12 was launched on March 19, 2019. It is a Non-LTS version. Hence it won’t have long term support.
Some of the important Java 12 features are;
Let’s look into all these Java 12 features one by one.
RedHat initiated Shenandoah Garbage Collector to reduce GC pause times. The idea is to run GC concurrently with the running Java threads. It aims at consistent and predictable short pauses irrelevant of the heap size. So it does not matter if the heap size is 15 MB or 15GB. It is an experimental feature in Java 12.
Stating Java 12, G1 will now check Java Heap memory during inactivity of application and return it to the operating system. This is a preemptive measure to conserve and use free memory.
Improvements in G1 efficiency include making G1 mixed collections abortable if they might exceed the defined pause target. This is done by splitting the mixed collection set into mandatory and optional. Thus the G1 collector can prioritize on collecting the mandatory set first to meet the pause time goal.
Microbenchmark Suite, JEP 230 feature adds a basic suite of microbenchmarks to the JDK source code. This makes it easy for developers to run existing microbenchmarks and create new ones. One AArch64 Port, Not Two, JEP 344, removes all of the sources related to the arm64 port while retaining the 32-bit ARM port and the 64-bit aarch64 port. This allows contributors to focus their efforts on a single 64-bit ARM implementation
This enhances the JDK build process to generate a class data-sharing (CDS) archive, using the default class list, on 64-bit platforms. The goal is to improve startup time. From Java 12, CDS is by default ON. To run your program with CDS turned off do the following:
java -Xshare:off HelloWorld.java
Now, this would delay the startup time of the program.
Java 12 has introduced many language features. Let us look at a few with implementations.
Java 12 has enhanced Switch expressions for Pattern matching. Introduced in JEP 325, as a preview language feature, the new Syntax is L ->
. Following are some things to note about Switch Expressions:
default
case is now compulsory in Switch Expressions.break
is used in Switch Expressions to return values from a case itself.Classic switch statement:
String result = "";
switch (day) {
case "M":
case "W":
case "F": {
result = "MWF";
break;
}
case "T":
case "TH":
case "S": {
result = "TTS";
break;
}
};
System.out.println("Old Switch Result:");
System.out.println(result);
With the new Switch expression, we don’t need to set break everywhere thus prevent logic errors!
String result = switch (day) {
case "M", "W", "F" -> "MWF";
case "T", "TH", "S" -> "TTS";
default -> {
if(day.isEmpty())
break "Please insert a valid day.";
else
break "Looks like a Sunday.";
}
};
System.out.println(result);
Let’s run the below program containing the new Switch Expression using JDK 12.
public class SwitchExpressions {
public static void main(String[] args)
{
System.out.println("New Switch Expression result:");
executeNewSwitchExpression("M");
executeNewSwitchExpression("TH");
executeNewSwitchExpression("");
executeNewSwitchExpression("SUN");
}
public static void executeNewSwitchExpression(String day){
String result = switch (day) {
case "M", "W", "F" -> "MWF";
case "T", "TH", "S" -> "TTS";
default -> {
if(day.isEmpty())
break "Please insert a valid day.";
else
break "Looks like a Sunday.";
}
};
System.out.println(result);
}
}
Since this is a preview feature, please ensure that you have selected the Language Level as Java 12 preview. To compile the above code run the following command:
javac -Xlint:preview --enable-preview -source 12 src/main/java/SwitchExpressions.java
After running the compiled program, we get the following in the console
Switch expressions is a preview language feature. This means that even though it is complete, it may not be confirmed in the future Java Release.
Java 12 added the following method to compare two files:
public static long mismatch(Path path, Path path2) throws IOException
This method returns the position of the first mismatch or -1L if there is no mismatch. Two files can have a mismatch in the following scenarios:
Example code snippet from IntelliJ Idea is given below:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
public class FileMismatchExample {
public static void main(String[] args) throws IOException {
Path filePath1 = Files.createTempFile("file1", ".txt");
Path filePath2 = Files.createTempFile("file2", ".txt");
Files.writeString(filePath1,"JournalDev Test String");
Files.writeString(filePath2,"JournalDev Test String");
long mismatch = Files.mismatch(filePath1, filePath2);
System.out.println("File Mismatch position... It returns -1 if there is no mismatch");
System.out.println("Mismatch position in file1 and file2 is >>>>");
System.out.println(mismatch);
filePath1.toFile().deleteOnExit();
filePath2.toFile().deleteOnExit();
System.out.println();
Path filePath3 = Files.createTempFile("file3", ".txt");
Path filePath4 = Files.createTempFile("file4", ".txt");
Files.writeString(filePath3,"JournalDev Test String");
Files.writeString(filePath4,"JournalDev.com Test String");
long mismatch2 = Files.mismatch(filePath3, filePath4);
System.out.println("Mismatch position in file3 and file4 is >>>>");
System.out.println(mismatch2);
filePath3.toFile().deleteOnExit();
filePath4.toFile().deleteOnExit();
}
}
The output when the above Java Program is compiled and run is:
import java.text.NumberFormat;
import java.util.Locale;
public class CompactNumberFormatting {
public static void main(String[] args)
{
System.out.println("Compact Formatting is:");
NumberFormat upvotes = NumberFormat
.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT);
upvotes.setMaximumFractionDigits(1);
System.out.println(upvotes.format(2592) + " upvotes");
NumberFormat upvotes2 = NumberFormat
.getCompactNumberInstance(new Locale("en", "US"), NumberFormat.Style.LONG);
upvotes2.setMaximumFractionDigits(2);
System.out.println(upvotes2.format(2011) + " upvotes");
}
}
Teeing Collector is the new collector utility introduced in the Streams API. This collector has three arguments - Two collectors and a Bi-function. All input values are passed to each collector and the result is available in the Bi-function.
double mean = Stream.of(1, 2, 3, 4, 5)
.collect(Collectors.teeing(
summingDouble(i -> i),
counting(),
(sum, n) -> sum / n));
System.out.println(mean);
The output is 3.0.
4 new methods have been introduced in Java 12 which are:
To know about the above methods and there implementation in detail, refer to our Java 12 String Methods tutorial.
A new package java.lang.constant
is introduced with this JEP. This is not that useful for those developers who don’t use constants pool.
Another Preview Language feature! The old way to typecast a type to another type is:
if (obj instanceof String) {
String s = (String) obj;
// use s in your code from here
}
The new way is :
if (obj instanceof String s) {
// can use s directly here
}
This saves us some typecasting which were unnecessary.
Raw String Literals is Removed From JDK 12.
That brings an end to this article on Java 12 features.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Is pattern matching for instance of part of Java12 and not 14?
- Asra
Excellent…! Good work, as always. And appreciate you getting this to us so quickly and precisely.
- Srikanth Vidiyala
Thank you, I really like your article!
- prd
very nice Thanks
- Madhav
Nice and precise!
- Ranjith Kumar Murugasamy
Thanks
- Vivek Naik