DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. Interesting JDK 12 Features to Watch Out For

Interesting JDK 12 Features to Watch Out For

There are several new features from JDK 12 to watch out for, including records and switch expressions. Let's take a closer look.

Lemuel Ogbunude user avatar by
Lemuel Ogbunude
·
Nov. 11, 18 · News
Like (25)
Save
Tweet
Share
42.46K Views

Join the DZone community and get the full member experience.

Join For Free

With the new six month Java release cycle, you should expect cool, new features for developers at a faster rate. I have seen some of the features that you can expect to see in the JDK 12 release next year. You can get the Open JDK 12 early access build and try out these preview features. Let's take a closer look.

Switch Expressions

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;
        };


Records (currently not targeted to JDK 12 at the time of this writing)

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 replaces all POJO code with just one line:

record Point(double x, double y);


There are other cool features that you could expect in JDK 12 and beyond. I, personally, appreciate that the Java community is evolving and aiming to move Java forward. With the new 6 months release cycle, we should expect cool features to get out faster, which is cool!


I suggest you check this video out: https://www.youtube.com/watch?v=nKJbDYRsO0s


Java (programming language) Java Development Kit

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Test Execution Tutorial: A Comprehensive Guide With Examples and Best Practices
  • 5 Steps for Getting Started in Deep Learning
  • How Elasticsearch Works
  • Getting a Private SSL Certificate Free of Cost

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: