Daniel Warren Daniel is a Java Developer at IDRsolutions and the product manager for FormVu. He enjoys experimenting with different computer systems, D&D, and a variety of PC games.

Are you a Java Developer working with PDF files?

Find out why you should be using JPedal

Java 12 Switch Expressions explained in 5 minutes

2 min read

upgrade

This month we are focusing on Java 12 new features you should know about. In other articles, we looked at improvements to Garbage Collection, Java 12’s JVM Constants API explained in 5 minutes, Java 12 made microbenchmarking easier

When Java 12 releases, we’re getting improvements to how switch cases are handled, soon we’ll not be limited to only using the C like the syntax of switch statements.

What does Java 12 want to do with Switch Expressions?

The goals listed in the JEP (325) are the following:

  • Extend switches so they can be a statement or an expression
  • Allow either “Traditional” or “Simplified” scoping/control flow behavior
  • Help prepare for instance of Pattern Matching (JEP 305)

What’s being added?

Expression label of ‘case L -> expression;’

Instead of having to break out of different cases, you can use the new switch label which allows the expression on the right to execute if the label matches. This assists in making code easier to read/understand in addition to make switch statements quicker to type.

An example would be:

switch (x) {
    case 1 -> System.out.println("Foo");
    default -> System.out.println("Bar");
}

Multiple case labels

Rather than forcing the fallthrough semantics of switch statements, Java 12 will allow you to list multiple case labels on the same line. This has been done to make code both easier to read and easier to understand.

An example of this in use would be:

switch (x) {
    case 1, 2, 4 -> System.out.println("Foo");
    default -> System.out.println("Bar");
}

Or you can use the “Traditional” colon syntax:

switch (x) {
  case 1, 2, 4:

    System.out.println("Foo");

    break;

  default:

    System.out.println("Bar");

    break;

}

Returning values from the switch statement

In a lot of circumstances switches are used to return specific values depending on the input provided, to support this Java 12 allows switch statements to return values. This removes the need to create a variable specifically for the purpose of returning a set value.

Example:

int y = switch (x) {
    case 1 -> 2;
    case 2 -> 4;
    case 3 -> 3;
    default -> 1;
};

Or you can include the value directly after break like so:

int y = switch (x) {
    case 1:
        break 2;
    case 2:
        break 4;
    case 3:
       break 3;
    default:
       break 1;
};

If you do end up using a switch as an expression in order to return a value, please note that you must have an exhaustive list of cases which either return a value or raise an exception. In the majority of circumstances, this means that you have to have a default case, but with enums, you just need to make sure you cover all known cases.

Better scoping

Due to fallthrough semantics, so far the scoping of switch statements have been the entire statement, meaning that two entirely separate cases could not use the same variable names. With the new changes, the scoping can now be used in a case level, allowing for cleaner code and some more flexibility.

This can be seen with the following:

int y = switch (x) {
  case 1:
    String temp = "first";
    break temp.length();
  case 2:
    String temp = "second";
    break temp.length();
  default:
    String temp = "none";
    break temp.length();
}

Where previously it would have to be:

int y = switch (x) {
  case 1:

    String firstTemp = "first";

    break firstTemp.length();

  case 2:

    String secondTemp = "second";

    break secondTemp.length();

  default:

    String defaultTemp = "none";

    break defaultTemp.length();

}

 

All in all, there is some nice quality of life changes coming to switch statements once Java 12 releases, be sure to give them a shot when you can.

 



Our software libraries allow you to

Convert PDF to HTML in Java
Convert PDF Forms to HTML5 in Java
Convert PDF Documents to an image in Java
Work with PDF Documents in Java
Read and Write AVIF, HEIC, WEBP and other image formats
Daniel Warren Daniel is a Java Developer at IDRsolutions and the product manager for FormVu. He enjoys experimenting with different computer systems, D&D, and a variety of PC games.