Enhancements and Features in Java 12

Enhancements and Features in Java 12

JDK 12 has been released in March 19 2019. The latest version of Java SE 12 JDK can be downloaded from https://www.oracle.com/technetwork/java/javase/downloads/jdk12-downloads-5295953.html.

Now Lets talk about some of the features and enhancement provided by Java SE 12

  1. Stream API Collectors.teeing()

This teeing() method has been used as static method in Collectors and collectors forwards its inputs before merging its result. Means two other collectors is being forwarded as inputs.

teeing(Collector, Collector, BiFunction) here you can see that two collectors and one function has been passed to merger the result . Every element passed to the resulting collector is processed by both downstream collectors, then their results are merged using the specified merge function into the final result.

For example, in a given list of employees, if we want to find out the employee with maximum salary and minimum salary, we can do it in single statement using teeing collector.

Collector.teeing() Example
SalaryRange salaryRange = Stream

.of(56700, 67600, 45200, 120000, 77600, 85000)





  1. String API Changes

2.1. String.indent()

The indent method helps with changing the indentation of a String. We can either pass a positive value or a negative value depending on whether we want to add more white spaces or remove existing white spaces.

String result = “foo\nbar\nbar2”.indent(4);




//    foo

//    bar

//    bar2

Please note that the indent() method automatically adds a newline character if it hasn’t been provided yet. That’s to be expected and is a feature of the new method.

Each white space character is treated as a single character. In particular, the tab character “\t” (U+0009) is considered a single character; it is not expanded.

2.2. String.transform()

The transform() method takes a String and transforms it into a new String with the help of a Function.

In given example, we have a list of names. We are performing two operations (trimming white spaces and making all names camel-case) using transform() method.

List<String> names = List.of(

”   Alex”,



List<String> transformedNames = new ArrayList<>();


for (String name : names)


String transformedName = name.transform(String::strip)





2.3. String constants

Since Java 12, String class implements two additional interfaces java.lang.constant.Constable and java.lang.constant.ConstantDesc.

String class also introduces two additional low-level methods describeConstable() and resolveConstantDesc(MethodHandles.Lookup).

They are low-level APIs meant for libraries and tools providing bytecode parsing and generation functionality, for example, Byte Buddy.

Just to note, a Constable type is one whose values are constants that can be represented in the constant pool of a Java class file as described in JVMS 4.4, and whose instances can describe themselves nominally as a ConstantDesc.

resolveConstantDesc() is similar to describeConstable() with the difference being that this method returns an instance of ConstantDesc instead.

  1. Files.mismatch(Path, Path)

Sometimes, we want to determine whether two files have the same content. This API helps in comparing the content of files.

mismatch() method compares two file paths and return a long value. The long indicates the position of the first mismatched byte in the content of the two files. The return value will be ‘–1’ if the files are “equal.”

Path helloworld1 = tempDir.resolve(“helloworld1.txt”);


Path helloworld2 = tempDir.resolve(“helloworld2.txt”);


long diff = Files.mismatch(helloworld1, helloworld2);   //returns long value

  1. Compact Number Formatting

Large numbers rendered by a user interface or a command-line tool are always difficult to parse. It is far more common to use the abbreviated form of a number. Compact number representations are much easier to read and require less space on the screen without losing the original meaning.

E.g. 3.6 M is very much easier to read than 3,600,000.

Java 12 introduces a convenient method called NumberFormat.getCompactNumberInstance(Locale, NumberFormat.Style) for creating a compact number representation.

NumberFormat formatter = NumberFormat.getCompactNumberInstance(Locale.US,



String formattedString = formatter.format(25000L);      //25K

  1. Support for Unicode 11

In a time in which emojis play a crucial role in communicating on social media channels, it’s more important than ever to support the latest Unicode specification. Java 12 has kept pace and supports Unicode 11.

Unicode 11 adds 684 characters, for a total of 137,374 characters – and seven new scripts, for a total of 146 scripts.

  1. Switch Expressions (Preview)

This change extends the switch statement so that it can be used as either a statement or an expression.

Instead of having to define a break statement per case block, we can simply use the arrow syntax. The arrow syntax semantically looks like a lambda and separates the case label from the expression.

With the new switch expressions, we can directly assign the switch statement to a variable.

boolean isWeekend = switch (day)




case SATURDAY, SUNDAY -> true;


default -> throw new IllegalStateException(“Illegal day entry :: ” + day);



System.out.println(isWeekend);  //true or false – based on current day

To use this preview feature, remember that we have to explicitly instruct the JVM during application startup using –enable-preview flag.