Java 12 Features | New Feature in Java

Feature 1: For example, you have an enum for the days of the week and you want to use a switch statement to return the number of letters in the string.

There are better ways to do this, but we are going to use Switch statements to demonstrate this.

Here is the enum:

enum DayOfWeek{
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
}

With what we know in Java, this would probably be the solution:

int numLetters;
switch(day){
case MONDAY: case FRIDAY: case SUNDAY:
numLetters=6;
break;
case TUESDAY:
numLetters=7;
break;
case THURSDAY: case SATURDAY:
numLetters=9;
break;
case WEDNESDAY:
numLetters=10;
break;
default:
throw new NoSuchDayException(day);
}

This could really trigger bugs, as it could make the code harder to maintain. With switch expressions, you can write the switch as an expression. No default is needed in the switch statement.

Here is what the code should look like:

 

int numLetters=switch(day){
case MONDAY, FRIDAY, SUNDAY -> 6;
case TUESDAY -> 7;
case THURSDAY, SATURDAY -> 8;
case WEDNESDAY -> 9;
}

Feature 2: POJOs

We all know our POJOs (plain old Java object); they come with a lot of boilerplate code.

Records replaces all POJO code with just one line:

record Point(double x, double y);

3. JEP 12: Preview Language and VM Features

A preview language or VM feature is a new feature of the Java SE Platform that is fully specified, fully implemented, and yet impermanent. It is available in a JDK feature release to provoke developer feedback based on real-world use; this may lead to it becoming permanent in the future Java SE Platform.

4. There is speculation of introducing objects without identity (value types) as well as moving towards 64-bit addressable arrays to support large data sets. (See wiki)