Author: Kokila Widyaratna
Date: 02/04/2024

Photo by bluebay2014 @ Getty Images
I started using Java in 2017 with version 8 where features like lambda expressions, stream API,
default method, optional wrapper..etc, were getting a huge hype. I’ve been incorporating almost
all those features during my day-to-day development work. Though I have been upgrading from
Java 8 to 11 and then to 17, I noticed that I have been using the same old features.
Of course, we are not bumping these Java versions only to get new language features but for
performance and security improvements as well.
But with the intention of getting to know new features in the language, I started doing a little bit
of exploring and found the following talk by Venkat Subramaniam which I recommend to every
Java developer. This post is inspired by this video.
Feature 1 — Switch Expression (Java 14)
Before going into this feature, let us look at the below code snippet and guess the console output.

The console output is;

Kudos if you guessed it correctly but this is a mistake which wasted a lot of my time during
my early years. If you look at the first case, you can see that the break statement is missing
and this is why the above code snippet is behaving in that manner.
Now let’s see how the switch expression feature which was introduced as a preview in
Java12 and then a standard feature in Java14, minimizes this error from happening.

And the output is;

console output of the example code snippet of the new switch expression feature
As you can see, a break statement is not required in the switch expression.
The ability to handle multiple cases is also a feature of the switch expression. An example
snippet and console output is given below. Please refer to line #6.


console output of the example code snippet for multiple case handling
The yield keyword is also something which was introduced with this switch expression feature
where we can use it to exit a switch expression by returning a value that becomes the value of
the switch expression. This means we can assign the value of a switch expression to a variable.
Refer to line #9 and the console output below as an example.


console output of the example code snippet for the ‘yield’ keyword
That is all for Part 1 of this series. In Part 2, I am planning to discuss the Java Records feature
which was introduced as a preview in Java 14 and then became a standard feature in Java 16.
Hope this can help! Share your thoughts too.

