Java 9 To 17 Upgrade Notes
Java 9 To 17 Upgrade Notes
© Akshita Chawla
fi
So what are we going to learn today
• Brief overview of Java versioning
• Java 17 - LTS : Sealed Classes
• Java 16 : Pattern Matching instance of
• Java 16 : Records
© Akshita Chawla
Another new version? What’s this LTS now?
© Akshita Chawla
Java 17 : Sealed Classes
Let’s say you have written a “beautiful” algorithm that you want only share with your team, but your code is also exposed to external users.
What do you ensure only “VIP” members get excess to your algorithm?
The final modi er on a class doesn’t allow anyone to extend it. What about when we want to extend a class but
only allow it for some classes?
This is where Java 17 comes into play with sealed classes. The sealed class allows us to make class effectively
nal for everyone except explicitly mentioned classes.
We added a sealed modi er to our Vehicle class, and we had to add the permits keyword with a list of classes that we allow to extend it.
After this change, we are still getting errors from the compiler.
There is one more thing that we need to do here.
We need to add final, sealed, or non-sealed modi ers to classes that will extend our class.
© Akshita Chawla
fi
fi
fi
fi
Java 16
&
Records,
© Akshita Chawla
Java 16 : Pattern Matching of instanceof
The pattern matching for instanceof operator avoids the boilerplate code to type test and cast to a variable more concisely
Pattern is a combination of
We don’t need line 6 and 12 anymore • A predicate that can be applied to a target
• A set of binding variables that are extracted from the target only if the predicate
successfully applies to it
© Akshita Chawla
Java 16 : Pattern Matching of instanceof
Scope of Variables
© Akshita Chawla
Java 16 : Record
If I asked how many PoJos have you written in your entire life, the
answer always be “Hell lot of it”
© Akshita Chawla
fi
Before Java 16
New Way
© Akshita Chawla
Java 15 : Text Blocks
Text block is an improvement on formatting String variables.
From Java 15, we can write a String that spans through several lines as regular text.
© Akshita Chawla
Java 15 : Text Blocks
A text block begins with three double-quote characters followed by a line terminator.
You can't put a text block on a single line, nor can the contents of the text block follow the three opening
double-quotes without an intervening line terminator.
© Akshita Chawla
Java 14 : Switch Expressions
Switch expressions allowed us to omit break calls inside every case block.
It helps with the readability of the code and better understanding
switch label “case L ->”, the switch block code looks clearer, more concise and more readable
© Akshita Chawla
Java 14 : Switch Expressions
If the code of a case is a block of code, you can use the yield keyword to return the value for the switch block.
© Akshita Chawla
Java 10 : Local Variable Type Inference
We declare local variables with non-null initializers with the var identi er, which can help you
write code that’s easier to read.
© Akshita Chawla
ff
fi
ff
Java 11 : Local Variable Type in Lambda Expressions
Java 11 introduced an improvement to the previously
mentioned local type inference.
This allows us to use var inside lambda expressions.
This was possible in Java 8 too but got removed in Java 10.
Now it’s back in Java 11 to keep things uniform.
But why is this needed when we can just skip the type in the lambda?
If you need to apply an annotation just as @Nullable,
you cannot do that without de ning the type.
Limitations
© Akshita Chawla
fi
Java 9 : Private Interface Methods
In Java 8, we can provide method implementation in Interfaces using Default and Static methods.
However we cannot create private methods in Interfaces.
To avoid redundant code and more re-usability, Oracle Corp is going to introduce private methods in Java SE 9 Interfaces.
From Java SE 9 onwards, we can write private and private static methods too in an interface using a ‘private’ keyword.
These private methods are like other class private methods only, there is no difference between them.
© Akshita Chawla
Java 9 : Try With Resources Improvement
Try with resources blocks are introduced from Java 7. In these blocks, resources used in try blocks are auto-closed. No need to close the resources explicitly.
But, Java 7 try with resources has one drawback.
It requires resources to be declared locally within try block. It doesn’t recognize resources declared outside the try block.
That issue has been resolved in Java 9.
We can now use nal or even e ectively nal variables inside a try-with-resources block:
Java 7
Java 9
© Akshita Chawla
fi
ff
fi
Java 9 : Diamond Operator for Anonymous Inner Class
Diamond operator was introduced as a new feature in java SE 7.
The purpose of diamond operator is to avoid redundant code by leaving the generic type in the right side of the expression.
Java 7 allowed us to use diamond operator in normal classes but it didn’t allow us to use them in anonymous inner classes.
Java 9 improved the use of diamond operator and allows us to use the diamond operator with anonymous inner classes.
Java 9
Java 7
© Akshita Chawla
© Akshita Chawla