DZone

According to some surveys such as that of JetBrains, version 8 of Java is currently the most used by developers all over the world, despite being a 2014 release.
What you are reading is an article in a series titled ‘Going beyond Java 8’, inspired by the contents of my book ‘Java for Aliens‘. These articles will guide the reader step by step to explore the most important features introduced starting from version 9. The aim is to make the reader aware of how important it is to move forward from Java 8, explaining the enormous advantages that the latest versions of the language offer.

In this article we will see how Oracle with Java 14 has officially introduced an evolution of the switch construct, which can now also be used as an expression, that is, it can return a value. This novelty is known as the switch expression. However, the introduction of switch expressions also caused the modification of the original construct, with the definition of a new word in the Java vocabulary (yield), and the possibility of using the arrow notation in this construct, which was introduced with the lambda expressions with Java 8. We will therefore distinguish the switch construct when used as a statement and as an expression, but rather than talking only about switch expressions, it seems more appropriate to talk about a renewed construct: the new switch, which can now be used as a statement or as expression.

Source: DZone