Java 12 features

There are some interesting and useful features available in Java 12. However some are in Preview stage, not for production use. All Preview features are labelled accordingly.

Compact Number Formatting

Java 12 comes with number formatter the CompactNumberFormat. It was designed to represent number in shorter form based on patterns provided by given locale.

NumberFormat shortNumber = NumberFormat.getCompatcNumberInstance(new Locale("en", "US"), NumberFormat.Style.SHORT);

shortNumber.format(1000);

output: 10k

Try what output you get when you try Style.LONG.

Teeing Collector in Stream API

Collector<T, ?, R> teeing(Collector<? super T, ?, R1> downstream1, Collector<? super T, ?, R2> downstream2, BiFunction<? super R1, ? super R2, R> merger)

Every element is processed by both the downstream collectors and their results are passed to merger function. The example of teeing collector is shown below

If we want to calculate average from set of numbers, we can use this.

Stream.of(1,2,3,4,5,6).collect(

Collectors.teeing(Collectors.summingDouble(i->i), Collectors.counting(), (sum, count) -> sum/count));

Switch (Preview)

One thing I like the most is, Switch statement or expression is undergoing some changes in it's syntax to accommodate more readability and reduce verbosity in code. I hope it comes out more efficient, readable and more optimized in future releases.

Let's dive into Switch statement little bit which is available as Preview in Java 12

Before Java 12 switch looks like below

String WEEK_DAY;

switch(WEEK_DAY) {
case Monday:
case Tuesday:
case Wednesday:
case Thursday:
case Friday:
    return "Business days";
case Saturday:
case Sunday:
   return "Non Business days";
}

But in Java 12 it is as simple as ..

String dayOfWeek;

switch(dayOfWeek) {
case Monday, Tuesday, Wednesday, Thursday, Friday -> return "Business days";
case Saturday, Sunday -> return "Non Business days";
}
If you want to accommodate more complex logic, embrace them within curly braces as below

switch(dayOfWeek) {
case Monday, Tuesday, Wednesday, Thursday, Friday -> {

    // More logic if you wanna add

    return "Business days";

}
case Saturday, Sunday -> return "Non Business days";
}

Please note: This new syntax is just an extension, not a replacement of old syntax. You can either use old or new syntax.

instaneOf (Preview)

Before Java 12 we should use instanceOf to check the instance type of the object and then type case it.

String obj = "This is a test";
if(obj instanceOf String) {
   String newObj = (String)obj;
    newObj .trim();
}

But with Java 12, it has become bit simpler.

String obj = "This is a test";
if(obj instanceOf String str) {
    obj = obj.trim();
}

There are some enhancements in JVM (Garbage Collector), some new methods are added to String API, minor updates in File nio classes. 

Conclusion:

Preview features in Java 12 are standardized and part of JDK 15. We can use JDK 15 if we want to use the preview features in production. Besides Preview features, we got a Compact Number Formatting and Teeing collector and also some other minor enhancements.

Comments

Popular posts from this blog

Bash - Execute Pl/Sql script from Shell script

How to get client Ip Address using Java HttpServletRequest

How to install Portable JDK in Windows without Admin rights