Java 12 Switch Expressions

java Feb 25, 2019

Oracle will release Java 12 in March and it comes with a handful of new features. I’m going to cover them in the next few posts. We’ll start this week with switch expressions. I’ll take a look at how they change how you’ll use the language.

A Basic Class

Let’s start with a simple car class.

public class Car {

    public enum Model {
        Standard,
        Deluxe,
        Limited
    }

    private final Model model;

    public Car(Model model) {
        this.model = model;
    }

    public String getModel() {
        return model;
    }
}

Car has an enum that indicates whether it’s a standard, deluxe, or limited model. We’ll going to use this flag to manage the car’s different features.

Standard Switch Statement

So, it makes sense that we’d use a switch statement to manage features.

Let’s set up a method to set the number of cylinders based on the model:

public int getCylinders() {

    int cylinders = 0;

    switch (model) {
        case Standard:
            cylinders = 4;
            break;
        case Deluxe:
            cylinders = 6;
            break;
        case Limited:
            cylinders = 8;
            break;
    }
    return cylinders;
}

We could write this without the local cylinders variable, of course. I started with an example that uses break statements to demonstrate a typical switch.

Here’s a simplified version:

public int getCylinders() {

    switch (model) {
        case Deluxe:
            return 6;
        case Limited:
            return 8;
        default:
        case Standard:
            return 4;
    }
}

Without the local variable, we need a default case to be sure a value is returned from the method.

Either way, switch statements are a little noisy.

Java 12 Switch Expressions

Java 12 makes this easier to write. You can use a switch expression:

public int getCylinders() {

    return switch (model) {
        case Standard -> 4;
        case Deluxe -> 6;
        case Limited -> 8;
    };
}

As the name implies, this new switch is an expression, not just a statement. It returns a value. So, the question of whether to use a local variable when selecting a case goes away.

It’s also much easier to read since we don’t need breaks, and each case fits on a single line.

But, you can still use multiple lines in a case if you need to. Let’s say you want to add another field to Car and use it to change the result of getCylinders(). You can add a code block inside the case, and use a break to return a value.

public int getCylinders() {

    return switch (model) {
        case Standard -> 4;
        case Deluxe -> {
            if (year == 1979) {
                break 4;
            } else {
                break 6;
            }
        }
        case Limited -> 8;
    };
}

Even though this adds a couple of break statements, it’s still cleaner and easier to follow than a switch statement.

I’m looking forward to moving to Java 12. Stay tuned for more!

Photo by Karim MANJRA on Unsplash

Great! You've successfully subscribed.
Great! Next, complete checkout for full access.
Welcome back! You've successfully signed in.
Success! Your account is fully activated, you now have access to all content.