January 29, 2020 /   java 14   jdk 14 features   new java features   java record

Java 14 (JDK 14) Features

JDK 14 is coming with some interesting set of features and some preview features.

Let's have a look on some features

Pattern Matching for instanceof

This will be a preview feature. Combining pattern matching with instance of will reduce the boilerplate code used to write in equal() methods or at similar situations.

Consider this equal method

public class Java14 {
    private String name;

    // THe equal method before java 14
    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Java14) {
            Java14 other = ((Java14) obj);
            return this.name.equalsIgnoreCase(other.name);
        }
        return false;
    }
}


Three things are happening here

The Pattern matching with a test and a target variable will be defined as obj instanceof Java14 other Above expression will test, convert and will provide us a variable other

Let's see how can we rewrite equal() method again using pattern matching

public class Java14 {

    private String name;

    // The equal method in java 14 with preview
    @Override
    public boolean equals(Object obj) {
        return  (obj instanceof Java14 other) && this.name.equalsIgnoreCase(other.name);
    }
}

Descriptive NullPointerException

This feature will add a description or hint of which variable caused NullPointerException

Example

Consider this code

public class Java14 {
    public static void main(String[] args) {
        Student student = new Student();
        student.address.pincode = 2321;
    }
}

class Student {
    Address address;
}
class Address {
    int pincode;
}


Above code will outputs

Exception in thread "main" java.lang.NullPointerException
    at Java14.main(Java14.java:4)

In Java 14, the same code may include some more details of exception and the message may look like

Exception in thread "main" java.lang.NullPointerException
        Cannot assign field "pincode" because "address" is null
    at Java14.main(Java14.java:4)


Java Records (preview)

Java Record will be released as preview feature. A record will hold immutable data and will implicitly provide basic functionality like getter methods. Goal of record is simple, to remove unnecessary boilerplate code. For example let's define a Range class which will have start and end boundaries.

class Range {
    private int start;
    private int end;
    Range(int start, int end) {
        this.start = start;
        this.end = end;
    }

    public int getEnd() {
        return end;
    }

    public int getStart() {
        return start;
    }

    @Override
    public String toString() {
        return "Range{" +
                "start=" + start +
                ", end=" + end +
                '}';
    }
}


If you are sure that you need only immutable instances of Range you may not chose to write that much of code in Java 14. This will be enough

record Range(int start, int end) {};


Above declaration will implicitly provide following things to each record instance