Java 12 and IntelliJ IDEA

Java 12 and IntelliJ IDEA

Java 12 and IntelliJ IDEA. In this article, we’ll cover the pain points of using existing switch statements, define switch expressions, and explain why they are good for you.

With switch expressions, Java 12 is enhancing one of its basic language constructs – switch – to improve everyday coding experience for developers. Benefits are multi-fold. As compared to the ‘traditional’ switch constructs, switch expressions can return a value. The ability to define multiple constants with a switch branch, and improved code semantics, makes it concise. By removing default fall-through switch branches, you are less likely to introduce a logical error in a switch expression.

Let’s get started.

Traditional switch constructs

If you think of a switch construct as a multi-way condition, using an expression seems to be a better fit. However, switch could only be used as a statement until now. The current switch syntax is constrained and verbose. It often leads to error-prone code that is difficult to debug.

Here’s an example that uses a switch statement to calculate the height of a chair, based on the size value passed to it:

public enum Size {S, M, L, XL};
public class Chair {
    public void calcHeight(Size size) {
        int height = 0; 
        switch (size) {
            case S:
                height = 18;
            case M:
                height = 20;
            case L:
                height = 25;

The preceding code has multiple issues:

  • Repetitive break and assignment statements add noise to code.
  • Code verbosity makes it difficult to comprehend the code.
  • Default fall-through in switch branches sneaks in a logical error – the missing break statement for case label S lets the control fall through to case label M. This results in assignment of 20 instead of 18 to height when you execute calcHeight(Size.S).

Switch expressions

Let’s rewrite the preceding example using a switch expression. In IntelliJ IDEA, you can use Alt+Enter on the switch keyword to see the suggestions. Select ‘Replace with enhanced ‘switch’ statement’ to convert the traditional switch statement to a switch expression:

The preceding code offers multiple benefits:

  • Code in a switch branch is concise and easy to read. You define what to execute to the right of ->.
  • Switch branches can return a value, which can be used to assign value to a variable.
  • Switch branches don’t need a break statement to mark their end. In absence of a break statement, the control doesn’t fall through the switch labels – which helps avoid logical errors.

Returning value vs. executing statements

When you aren’t using a switch expression to return a value, a switch branch can choose to execute a statement or block of statements, or even throw an exception:

public enum Size {S, M, L, XL};
public class NotReturningValueFromSwitchLabel {
    public void calcHeight(Size size) {
        int height = 0; 
        switch (size) {
            case S -> height = 18;
            case M -> { 
                           height = 20; 
            case L -> height = 25;

Handling all possible argument values

When you are using a switch expression to return a value, it should be able to handle all possible values that you could pass to it as an argument. For instance, if you miss a case label corresponding to an enum constant, IntelliJ IDEA detects it. It offers to insert the specific case label or default case label.

By default, IntelliJ IDEA inserts a default value depending on the variable type. You can edit the placeholder value:

Types other than an enum can have infinite values. When you pass types like byte, short, int, or String and miss including the default label, IntelliJ IDEA can detect and fix it:

Define multiple constants in the same case label

Unlike switch statements, switch expressions allow you to define comma-separated multiple constants in a case label. This cuts down code redundancy – you can execute the same code for all case labels.

If you define redundant code for your case labels in a switch expression, IntelliJ IDEA can fix it for you:

Local variables and break statements in switch branches

With switch expressions, you can define local variables in switch branches. The block must include a break statement specifying the value to return:


enum Size {S, M, L, XL};

public class LocalVariablesWithSwitch { public void assignValue(Size size) { int height = 0; height = switch(size) { case S -> 18; case M -> { int weight = 19; break (weight > 10 ? 15 : 20); } case L, XL -> 25; }; } }

The case label for value M defines a block statement. A block can also define local variables (weight in this case). The scope and accessibility of the local variable weight is limited to the case label M. Notice how a switch expression uses a break statement to return a value.

Just in case you miss defining a return value for a switch branch, IntelliJ IDEA underlines the keyword case. When you hover the mouse pointer over it, you can view the message about the missing return value:

Preview language feature

Switch expressions is a preview language feature. This essentially means that even though it is complete, it has a possibility of not being confirmed as a permanent feature in a future Java release. This happens for a reason.

Java runs on billions of devices and is used by millions of developers. Risks are high for any mistake in a new Java language feature. Before permanently adding a language feature to Java, the architects of Java evaluate what the developers have to say about it – how good or bad it is. Depending on the feedback, a preview feature might be refined before it’s added to Java SE, or dropped completely. So, if you have any feedback on Switch expressions, please share it here.

IntelliJ IDEA Configuration

Since Switch expressions is a Java 12 language feature, please download and install OpenJDK 12 and configure it to use with IntelliJ IDEA:

Java is evolving and switch expressions is one of the welcome changes in Java 12.

Happy Coding!

Originally published by Mala Gupta at

java intellij-idea

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

IntelliJ IDEA for Beginners - Learn IntelliJ IDEA from Scratch

IntelliJ IDEA for Beginners - Learn IntelliJ IDEA from Scratch. How to become an expert IntelliJ IDEA developer. Learn how to use all the features of Intellij IDEA 2020. Complete IntelliJ IDEA 2020 course for Beginners.

Using Java 14 with IntelliJ IDEA

Using Java 14 with IntelliJ IDEA. What's New in IntelliJ IDEA 2020? What's New in Java 14? Java 14 has a lot of exciting language features for us developers: Records, Pattern Matching for instanceof, Text Blocks, Switch Expressions

How to Make IntelliJ IDEA look Amazing!

Adding Material Theme to IntelliJ Idea: In this video we will see how to add material theme to IntelliJ Idea Java IDE.

Creating your first Java Application with IntelliJ IDEA

In this Java tutorial, you will learn how to create, run, and package a simple Java application that prints Hello, World! to the system output. Along the way, you will get familiar with IntelliJ IDEA features for boosting your productivity as a developer: coding assistance and supplementary tools.