Java 14 – Switch Expression

The official standard release of the switch expression in Java 14, as it was a previews in Java 12 & 13.  

Java 12 preview: introduced the lambda syntax  

Java 13 preview: introduced the yield statement. 

General Q & A 

What are the changes with the switch expression? 

Example 

Code Example can be found here.

Old Style

Uses break statements between case statements. This leads to verbosity of code as well as you might have accidental fall-through/ forgetting to add the break statements.  

New style  

Lambda style syntax is introduced.  

Adding of the yield statement which returns values.   

Final Thoughts 

In my opinion, this helps in code readability especially if you are using a lot of lambdas in your code.  

One thought on “Java 14 – Switch Expression

Leave a Reply

Your email address will not be published. Required fields are marked *