Java 17 for the impatient
I have been working on Java 8 for a long time and now Java 17 has been released so let's have a quick look at this article which focuses on the latest Java 17 LTS features.
There are lots of features and improvements that have been introduced, however, in this article, we will focus on a limited set as listed below
- Record classes
- Sealed Class
- Switch case
- Enhanced instanceOf Operator
- Compact Number Format
- Text Blocks
- Stream toList
Records classes
Records classes are immutable and used as data transfer object with zero boilerplate code. Record classes are defined with the record keyword. It always extends the java.lang.Record
class.
record Country(String name,String capital){}
The above line of code generates functionality automatically like
- All fields are
private
andfinal.
- A canonical constructor for all fields.
- Public accessor for all fields.
equals
,hashcode
, andtoString
methods.
Also, it can be defined locally either inside class or a method.
Sealed Class
A sealed class specifically lists the permitted direct subclasses. It won’t allow any other class to extend.
Though the permits keywords can be removed if all the classes are defined in the same file.
Switch case
The switch statement in java has evolved over time. At first, It allowed only primitive then it introduced String. In Java 17 we have pattern matching of instanceOf with switch case which allows us to pass different types of complex objects.
The switch statement can now be written like this
switch statement with yield
Another way to write switch case with blocks where we can add logic inside the block like this
switch statement with the return
Also, the switch statement can now return value, it can be written like this
switch statement with type coverage
In java 17 we can also check for the type of selectors as sealed class as we have seen above. It does not need a default case because it does the type coverage with all permitted classes.
In case if it does not cover all the possible values then the compiler will throw an error. As we can see in the example below the ClassOne
type is missing. So it will generate a compile-time error.
Enhanced instaceof Operator
Enhanced instaceof operator allows you to pattern matching and eliminates extra line of code which was required earlier to perform casts
In the above snippet, the scope of the variable countryData
is specific to if block. The variable in scope can be used to match the pattern strictly like this
It is important to keep in my mind that the variable inside the scope should not be ambiguous. The condition && above can only be executed successfully after checking the result of instanceOf
the result as true
. If we change the &&
to ||
the code will not compile.
Compact Number Format
CompactNumberFormat class is the subclass of NumberFormat class in java.text
package, It is responsible to formats a number in compact form. A factory method is added to NumberFormat to format numbers in compact, human-readable form. There are two style of formats, LONG and SHORT as shown below
The output of the above code will look like this
The output of the above code will look like this
Text Blocks
Text Blocks are used to make the code more readable. It allows us to write multi-line strings. A Text Block is started with three quotes followed by the line break and closed by three quotes as shown below
Streams toList
Earlier to convert a stream to a list we need to use the collect
method with Collectors.toList()
. But now in Java 17, we can directly call toList()
method on stream object as mentioned below.
These are the must-know features for a Java developer in their daily work. In the next blog, we will look at other performance improvement features introduced in JAVA 17 LTS.
Till then Happy Coding.