Java 12, finally less verbose?

We all know Java for its characteristics thanks to which, despite more than 20 years have passed since the first version, it is still one of the most studied and most used languages, despite the growing diffusion of Python.

However, one of the biggest flaws is the fact that it is very verbose and for years Oracle has not shown any signs of change. Finally, with version 12 something seems to change. Java seems to be moving towards a way of programming that, in a future that still seems far away, allows us to write more compact code without losing legibility and above all maintaining the portability that has always characterized this language.

Let's see some important news.

Switch expression improvements

The foundamental thing that must be said is that this feature has been introduced as an experimental function in JEP 325, which means that even if the implementation is complete and working, may not be confirmed int the next versions.

What are the foundamental news?

  • Using break for every case is not necessary anymore;
  • Different constants can be used for the same case;
  • The default case is compulsory;
  • The break instruction can be used to return the switch values;

Let's look at an example:

String season= "";
switch (month) {
case "March":
case "April":
case "May": {
stagione= "Spring";
case "June":
case "July":
case "August": {
stagione= "Summer";

While now, with the new syntax, the code becomes: 

String season=switch(month){
     case "March", "April", "May":{
          break "Spring";
     } case "June", "July", "August":{
          break "Summer";
     }default ->{
          break "Other season";

This new way of writing switch expressions could be very comfortable.

New way to compare files: File.mismatch()

The method signature is:

public static long mismatch(Path path, Path path2) throws IOException

The idea is to return the distance between the two files, returning -1 if the two files are the same. Two files can be different when:

  • There are some different bytes, then the position of the first different byte is returned;
  • The file size is not the same, then the minor is returned;

Is this useful? Absolutely yes!

Compact number notation

Veongono introdotti nuovi metodi per la notazione compatta dei numeri a seconda del locale scelto.

New method for the compact number notation depending on the locale chosen are introduced.

public class CompactNumberFormatDemo
private static void exampleCompactNumberFormatting(final long numberToFormat){
final NumberFormat nfDefault = NumberFormat.getCompactNumberInstance();
final NumberFormat nfItShort =  NumberFormat.getCompactNumberInstance(Locale.ITALY, NumberFormat.Style.SHORT);
final NumberFormat nfItLong = NumberFormat.getCompactNumberInstance(Locale.ITALY, NumberFormat.Style.LONG);
final NumberFormat nfFrShort = NumberFormat.getCompactNumberInstance(Locale.FRANCE, NumberFormat.Style.SHORT);
final NumberFormat nfFrLong = NumberFormat.getCompactNumberInstance(Locale.FRANCE, NumberFormat.Style.LONG);
out.println("Number to format '" + numberToFormat + "':");
out.println("tDefault:  " + nfDefault.format(numberToFormat));
out.println("tIT/Short: " + nfItShort.format(numberToFormat));
out.println("tIT/Long: " + nfItLong.format(numberToFormat));
out.println("tFR/Short: " + nfFrShort.format(numberToFormat));
out.println("tFR/Long:  " + nfFrLong.format(numberToFormat));
public static void main(final String[] arguments)

We will obtain:

Number to format '15000':

Default:  15K
IT/Short: 15.000
IT/Long:  15 mila
FR/Short: 15 k
FR/Long:  15 mille

Teeing collector

The basic principle is similar to the tee command, familiar to unix users, that redirects the input to the two collectors before merging their results using a Bi-function.

var result = Stream.of("Andreas", "Antonia", "Lucia",
// primo collector
Collectors.filtering(n -> n.contains("c"), Collectors.toList()),
// secondo collector
Collectors.filtering(n -> n.endsWith("s"), Collectors.toList()),
// merger Bi-function
list1, List
list2) -> List.of(list1, list2)
System.out.println(result); // -> [[Lucia, Francesco], [Andreas]]

JEP 305: Pattern Matching for instanceof (Preview)

Finalmente non è più necessario effettuare il casting esplicito prima di usare un oggetto prima di poterlo utilizzare.

Making an explicit cast is not necessary anymore before using an object.

	// Before java 12 
if (obj instanceof String) {
String s = (String) obj;
// use s as a string
// In java 12 and maybe then...
if (obj instanceof String s) {
// using s as a string is possible without any explicit cast

Other news

  • New method has been added to the String class.More information can be found on the official documentation :
    • indent(int n);
    • transform(Function f);
    • Optional describeConstable();
    • String resolveConstantDesc​(MethodHandles.Lookup lookup);
  • With JEP334 a new class java.lang.constant has been introduced. It contains the nominal descriptors of various types of constants.

Java seems to be moving towards a syntax that will make the code easier, even if the most interesting features are still a preview. Personally, I am so curios to follow the developements of this version.

I wouldn't have kept the use of variables var, used from java 10. This variables allow the programmer not to define a specific variable type, a little bit like JavaScript. Despite of this, I recognize that sometimes they could be useful, even if they can be used only as local variables. Nowadays Java continues not to interpret var as a keyword, therefore a word dedicated to language, but it still recognizes its meaning. Who knows, maybe with the passage of time and generations of programmers, the use will become more frequent and will become a keyword.

Dealing with dates, what are the dates that concern with the various releases?

  • 13/12/2019 First distribution phase
  • 17/01/2019 Second distribution phase
  • 07/02/2019 Beta version release
  • 19/03/2019 General availability

Is it worth switching to Java12?

In my opinion, absolutely yes! Expecially for the programmers that want to try new features as soon as possible. Personally, I would do the upgrade again a thousand times! I found out some improvements, maybe due to the garbage collector algorithm change, called Shenandoah that doesn't take care of the heap dimension.

Quite the opposite, for beginners it might be convenient to start with the previous version, which is certainly more stable and has no degree differences. When the version becomes stable with the various definitive features, it will certainly be interesting to upgrade.

Enjoy your programming experience with Java! wink

by Alessio Mungelli Date: 10-10-2019 java jdk developing programming jdk12 software programmers tutorials explanation whatsnew hits : 3802  
Alessio Mungelli

Alessio Mungelli

Computer Science student at UniTo (University of Turin), Network specializtion, blogger and writer. I am a kind of expert in Java desktop developement with interests in AI and web developement. Unix lover (but not Windows hater). I am interested in Linux scripting. I am very inquisitive and I love learning new stuffs.


Related Posts

Node.js and npm: introductory tutorial

In this tutorial we will see how to install and use both Node.js and the npm package manager. In addition, we will also create a small sample application. If you…

How to connect to MySQL with Node.js

Let's see how you can connect to a MySQL database using Node.js, the popular JavaScript runtime environment. Before we start, it is important to note that you must have Node.js installed…

JavaScript Programming Styles: Best Practices

When programming with JavaScript there are certain conventions that you should apply, especially when working in a team environment. In fact, it is common to have meetings to discuss standards…

Difference between arrow and normal functions in JavaScript

In this tutorial we are going to see how arrow functions differ from normal JavaScript functions. We will also see when you should use one and when you should use…

JavaScript Arrow functions: What they are and how to use them

In this article we are going to see what they are and how to use JavaScript Arrow Functions, a new feature introduced with the ES6 standard (ECMAScript 6). What are Arrow…

How to insert an element into an array with JavaScript

In this brief tutorial you will learn how to insert one or more elements into an array with JavaScript. For this we will use the splice function. The splice function will not…

What is the difference between primitives types and objects in JavaScript?

In this short tutorial we are going to look at the differences between primitive types and objects in JavaScript. To start with, we're going to look at what primitive types…

How to get DOM elements with JavaScript

When you access any element of the DOM, it is usual to save it in a variable. This is something that at first might seem very simple, but if you…

How to reverse an array in JavaScript

In this tutorial we are going to see how you can change the order of the elements of an array so that they are inverted. You could use a loop…

How synchronize the scroll of two divs with JavaScript

In case you have two divs of different sizes you may sometimes want to scroll both at the same time but at different speeds depending on their size. For example,…

How to use the codePointAt method in JavaScript

The JavaScript codePointAt method has more or less the same function as the charCodeAt method, used to get the 16-bit Unicode representation of the character at a certain position in…

How to check if a value is a number in JavaScript

In this short tutorial we are going to look at the various methods that exist to find out if a value is a number in JavaScript.   1. Using the isNaN() function   One…

We use our own and third-party cookies to improve our services, compile statistical information and analyze your browsing habits. This allows us to personalize the content we offer and to show you advertisements related to your preferences. By clicking "Accept all" you agree to the storage of cookies on your device to improve website navigation, analyse traffic and assist our marketing activities. You can also select "System Cookies Only" to accept only the cookies required for the website to function, or you can select the cookies you wish to activate by clicking on "settings".

Accept All Only sistem cookies Configuration