Java 12: Switch Expressions

Denuwan Himanga Hettiarachchi
3 min readMar 20, 2019

--

Hi Falks,

With the new Java release schedule, java makes breaking news in developers life in every 6 months. Yesterday (19-March-2019) Oracle released Java 12 JDKs in both SE and OpenJDK editions. Not like the previous editions starting with JDK 11 Oracle has updated the license terms on which they offer the Oracle JDK. The new Oracle Technology Network License Agreement for Oracle Java SE is substantially different from the licenses under which previous versions of the JDK were offered. You have to review the new terms carefully before downloading and using this product. Otherwise, Larry Ellison will definitely sue you.

This post will primarily focus on the OpenJDK release features. With this new release, Oracle announced following impressive features are wrap with their newest OpenJDK.

01. Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
02. Microbenchmark Suite
03. Switch Expressions (Preview)
04. JVM Constants API
05. One AArch64 Port, Not Two
06. Default CDS Archives
07. Abortable Mixed Collections for G1
08. Promptly Return Unused Committed Memory from G1

In the day-to-day development life of the Java developers, switch Expression is one of the groundbreakings features available in Java 12 after lambda expressions in Java 8. In this post, I’ll explain everything you need to know about Switch Expressions.

The current design of Java switch statement more likely C and C++. This would lead to error-prone and visually complex code, unnecessarily break statement make the code more complicate and maintainability risk. Let’s look at the following traditional switch code segment.

Traditional Switch Expression

In Java 12 introduce a new form of switch label, “case L ->” will simplify the above switch block into following less complex block.

Java 12 Switch Expression

But when you compile the above code, you will be getting the error that says,

SwitchDemo.java:32: error: multiple case labels are a preview feature and are disabled by default.
case "JDK Beta", "JDK 1.0", "J2SE 5.0", "Java SE 6", "Java SE 7", "Java SE 9", "Java SE 10" -> System.out.println("Java Old Version");
^
(use --enable-preview to enable multiple case labels)
SwitchDemo.java:32: error: switch rules are a preview feature and are disabled by default.
case "JDK Beta", "JDK 1.0", "J2SE 5.0", "Java SE 6", "Java SE 7", "Java SE 9", "Java SE 10" -> System.out.println("Java Old Version");
^
(use --enable-preview to enable switch rules)
2 errors

You need to compile and run the code using the following commands,

javac —-enable-preview -source 12 SwitchDemo.javajava --enable-preview SwitchDemo

In additional to “traditional” switch statement, the switch is not just a statement anymore. We can use the switch as expression not only “case L->” but with traditional “case L:”. But in switch statement you have to define default option, otherwise, you will get a compilation error. This might be reduced unisensory code complication and improve maintainability in java code. Just look at how easy of it.

Switch Expression

You may note, the default block in switch expression with the new release they have extended the break statement to take an argument, which becomes the value of the enclosing switch expression. You can use these default block only when you use switch as an expression. Can’t use default block in switch statement.

Hope you got an extensive idea about Java 12: Switch Expressions.

Happy Coding…

--

--

Denuwan Himanga Hettiarachchi
Denuwan Himanga Hettiarachchi

Written by Denuwan Himanga Hettiarachchi

Blogger, Freelance Writer, and Tech Lead based in Colombo, SL.

No responses yet