DEV Community

loading...
Cover image for Interesting JDK 12 features to watch out for.

Interesting JDK 12 features to watch out for.

lemuelogbunude profile image Lemuel Ogbunude ・2 min read

With the new 6 months Java release cycle, you should expect cool features to get to developers at a faster rate. I have seen some cools features which you could expect to see in the JDK 12 release next year. You can get the Open JDK 12 early access build and try these preview features out.

Switch expressions

For example, you have an enum for the days of the week and you want to use 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 some bugs as it could make the code hard to maintain.
With Switch Expressions, you could write the Switch as an expression. No default is needed in the Switch statement.

Here is how the code would look like:

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

Records

We all know our POJOs (plain old Java object), they come with a lot of boilerplate code, for example here is a classic POJO:

class Point {
    double x;
    double y;

    Point(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public void setX(double x) {
        this.x = x;
    }

    public double getX() {
        return x;
    }

    public void setY(double y) {
        this.y = y;
    }

    public double getY() {
        return y;
    }

    @Override
    public int hashCode() {
        return super.hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        return super.equals(obj);
    }


    @Override
    public String toString() {
        return super.toString();
    }
}

Well, Records (currently not targeted to JDK 12 at the time of this writing) replace all that POJO code with just one line:

 record Point(double x, double y);

There are other cool features to expect in JDK 12 and beyond, I personally appreciate the fact that the Java community is responding and aiming to move Java forward.

In case you are interested in learning way more you could check out this video:

Discussion

pic
Editor guide
Collapse
emlautarom1 profile image
Martín Emanuel

Thank God for 'record'. Adding and maitaining get/set/equals/etc is always an unnecesary pain for us. At first glance this looks like Kotlin, and I really like it!

Collapse
gazsiadam profile image
Adam Gazsi

Have you tried Lombok? Does the same thing, and even more. projectlombok.org/

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

Thanks for the info Adam. I didn't know about Lombok! Looks really cool.

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

Haha, yeah at first glance it does look like Kotlin. Glad you like it.

Collapse
lysofdev profile image
Esteban Hernández

StuckWithJava8 :(

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

Trust me, I know that feeling :(

Collapse
simonhaisz profile image
simonhaisz

Record is not listed as a proposed JEP for 12: openjdk.java.net/projects/jdk/12/

In fact I cannot find it anywhere in the JEP list: openjdk.java.net/jeps/0

EDIT: I scanned through the video to find the section on record - unlike Switch Expressions and Raw String Literals there was no slide that provided a JEP. Looks like it's more of a JDK 13+ thing, don't expect it in 12.

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

Sorry I didn't mention that Records are currently not in the preview features, was probably too excited :) Thank you for that.
Currently, there are 4 JEPs targeted to JDK 12 so far which are Switch Expressions, Raw String literals and two others.
It might be more of a JDK 13+ thing as you said though with the faster 6 months release cycle that wouldn't be an issue of waiting for 3+ years any longer :)

Collapse
gklijs profile image
Gerard Klijs

It is when your company sticks to lts releases, witch is still better than companies stuck on Java 8.

Collapse
marconicolodi profile image
Marco Nicolodi

I would love to have this record feature in c#

Collapse
saint4eva profile image
saint4eva

You should check the upcoming C#

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

I'm sure that would be awesome...

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

Yeah, it's a nice feature, probably could be added as the language evolves.

Collapse
ozeta profile image
Marco Carrozzo

record seems really useful. but what about class extention? how will this work? it will support override,overload?

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

Yes, you can override the methods you want to and customise it to your taste, in the video I provided there was a demo showing that you could also override a method, for example, ToString() to your requirement.

Collapse
ahierro profile image
ahierro

They are getting close to Scala now. I hope they do something about pattern matching in future releases.

Collapse
lemuelogbunude profile image
Lemuel Ogbunude Author

You could expect cool features anytime soon :)