We are a Melbourne-based nearshore software outsourcing company providing world-class dedicated tech talent and development expertise to companies listed in the Australian Share Market to innovative startups who are shaping the future.

Contacts Us

Melbourne
Suite 37/11, Wilson St,
South Yarra,
VIC 3141.

Colombo
19, Katukurunduwatta Road, Ratmalana,

info@unibench.com.au

Melbourne
1300 320 487

Colombo
+94 769 360 433

Development

New Java language features worth checking out!
(2024) — Part 1

Author: Kokila Widyaratna
Date: 02/04/2024

image-1-6 New Java language features worth checking out! <br> (2024) — Part 1

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.

1-1 New Java language features worth checking out! <br> (2024) — Part 1

The console output is;

image-1-7 New Java language features worth checking out! <br> (2024) — Part 1

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.

2 New Java language features worth checking out! <br> (2024) — Part 1

And the output is;

image-1-8 New Java language features worth checking out! <br> (2024) — Part 1

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.

3 New Java language features worth checking out! <br> (2024) — Part 1

image-1-9 New Java language features worth checking out! <br> (2024) — Part 1

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.

4 New Java language features worth checking out! <br> (2024) — Part 1

image-1-10 New Java language features worth checking out! <br> (2024) — Part 1

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.

Leave a comment

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