# Java switch - 4 wrongs don't make a right

DevFeed: [Java switch - 4 wrongs don't make a right](<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: [conditionals](<https://devfeed.tech/topics/conditionals.md>), [enum](<https://devfeed.tech/topics/enum.md>), [if statement](<https://devfeed.tech/topics/if-statement.md>)

Tags: [complex](<https://devfeed.tech/tags/complex.md>), [design](<https://devfeed.tech/tags/design.md>), [expression](<https://devfeed.tech/tags/expression.md>), [fall](<https://devfeed.tech/tags/fall.md>), [java](<https://devfeed.tech/tags/java.md>), [project](<https://devfeed.tech/tags/project.md>), [project-amber](<https://devfeed.tech/tags/project-amber.md>), [return](<https://devfeed.tech/tags/return.md>), [switch](<https://devfeed.tech/tags/switch.md>)

## AI overview

The author argues that Java's planned switch upgrade adds complexity around the useful arrow-form switch expression. The classic switch can fall through when `break` is omitted, shares one variable scope across cases, and does not require `default`. The proposal adds four forms: classic and arrow syntax, each as a statement or expression. Arrow forms avoid fall-through; expressions must be exhaustive, while statement forms need not be. The author sees the classic-syntax expression as preserving fall-through risks and the arrow statement as allowing silent omissions, and questions whether orthogonality justifies both. They favor keeping only the arrow expression, allowing it to serve as a statement expression, and introducing `match` as its keyword. The excerpt also notes that switch expressions cannot return early or continue/break a loop.

## 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