# Java switch statement redesign: benefits and complexity

DevFeed: [Java switch statement redesign: benefits and complexity](<https://devfeed.tech/articles/java-switch-4-wrongs-don-t-make-a-right-22009.md>)

Original publisher: [Read original article](<http://blog.joda.org/2019/11/java-switch-4-wrongs-dont-make-right.html>)

Author: Stephen Colebourne (noreply@blogger.com)

Published: 2019-11-04T20:32:00Z

Content type: opinion

Language: en

Sources: [Stephen Colebourne](<https://devfeed.tech/sources/stephen-colebourne.md>)

Topics: [Java](<https://devfeed.tech/topics/java.md>), [upgrade](<https://devfeed.tech/topics/upgrade.md>), [Code](<https://devfeed.tech/topics/code.md>)

Tags: [code](<https://devfeed.tech/tags/code.md>), [coding](<https://devfeed.tech/tags/coding.md>), [java](<https://devfeed.tech/tags/java.md>), [upgrade](<https://devfeed.tech/tags/upgrade.md>)

## AI overview

This opinion article examines planned changes to Java's switch statement, including expression forms and new arrow syntax. It argues that the redesign addresses some classic switch problems but introduces substantial complexity and awkward syntax.

## Source excerpt

The switch statement in Java is being changed. But is it an upgrade or a mess? Classic switch The classic switch statement in Java isn't great. Unlike many other parts of Java, it wasn't properly rethought when pulling features across from C all those years ago. The key flaw is "fall-through-by-default". This means that if you forget to put a break clause in each case, processing will continue on to the next case clause. Another flaw is that variables are scoped to the entire switch, thus you cannot reuse a variable name in two different case clauses. In addition, default clause is not required, which leaves readers of the code unclear as to whether a clause was forgotten or not. And of course there is also the key limitation - that the type to be switched on can only be an integer, enum or string. String instruction; switch (trafficLight) { case RED: instruction = "Stop"; case YELLOW: instruction = "Prepare"; break; case GREEN: instruction = "Go"; break; } System.out.println(instruction); The code above does not compile because there is no default clause, leaving instruction undefined. But even if it did compile, it would never print "Stop" due to the missing break. In my own coding, I prefer to always put a switch at the end of a method, with each clause containing a return to reduce the risks of switch. Upgraded switch As part of Project Amber, switch is being upgraded. But sadly, I'm unconvinced as to the merits of the new design. To be clear, there are some good aspects, but overall I think the solution is overly complex and with some unpleasant syntax choices. The key aim is to add an expression form, where you can assign the result of the switch to a variable. This is rather like the ternary operator (eg. x != null ? x : ""), which is the expression equivalent of an if statement. An expression form would reduce problems like the undefined variable above, because it makes it more obvious that each branch must result in a variable. The current plan is to add no