0% found this document useful (0 votes)
14 views

Java 9 To 17 Upgrade Notes

The document discusses new features introduced in various versions of Java from Java 9 to Java 17, including sealed classes in Java 17, records and pattern matching in Java 16, text blocks in Java 15, switch expressions in Java 14, and local variable type inference in Java 10. It also discusses the new LTS release cycle for Java starting from Java 11.

Uploaded by

hoda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Java 9 To 17 Upgrade Notes

The document discusses new features introduced in various versions of Java from Java 9 to Java 17, including sealed classes in Java 17, records and pattern matching in Java 16, text blocks in Java 15, switch expressions in Java 14, and local variable type inference in Java 10. It also discusses the new LTS release cycle for Java starting from Java 11.

Uploaded by

hoda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Java 9 to 17

Let’s truly upgrade!

Akshita Chawla | 29th Aug


Although we have upgraded
to Java 17 in our pom les
but our code looks like this

© 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

• Java 15 : Text Blocks

• Java 14 : Switch Expressions

• Java 10 : Local Variable Type Inference

• Java 11 -LTS : Local Variable Type in Lambda Expressions


• Java 9 : Private Interface Methods

• Java 9 : Diamond Operator for Anonymous Inner Class

• Java 9 : Try With Resources Improvement

© Akshita Chawla
Another new version? What’s this LTS now?

In June 2018, just , Oracle announced a change to the


release cadence model for Java SE.

Rather than having a major release planned for every


two to three years (which would often become three to
four years), a new six-month feature-release-train model
would be used: Every three years, a release would be
designated as Long-Term Support (LTS) and receive
quarterly security, stability, and performance updates
only.

This pattern borrowed shamelessly from the Mozilla


Firefox release model but tweaked it to be more aligned
with the requirements of a development platform.

LTS version Six Month Release Cycle


• Java 8
• March
• Java 11
• September
• Java 17
• Java 21 (Releasing on 19th Sept)

© 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

With features like

&
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

Before Java 16 After Java 16

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

The assignment of pattern variable happens


only when the predicate test is true.

© Akshita Chawla
Java 16 : Pattern Matching of instanceof
Scope of Variables

The print statement will execute only when the


predicate fails, so this statement is not even compiled.

The variable is available in the enclosing if block,


and we cannot access it outside it.

If we are writing complex conditional statements in the if-statement then


we can use the pattern variable only with && (AND) operator because the
variable will be accessed only when the predicate has been tested to be
true. We cannot use the variable with || (OR) operator because the
predicate may be tested false in a few cases, and then we will be
exceptions of different kinds.

© 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”

Java has had a bad reputation for boilerplate code.


Lombok allowed us to stop worrying about getters, setters, etc.
Java 16 nally introduced records to remove a lot of boilerplate code

Records provide a compact syntax for declaring


classes which are plain immutable data carriers

© 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.

Before Java 15 After Java 15

© 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.

How can we use it in our code

© 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

Before Java 14 After Java 14

© 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.

Java always needed explicit types on local variables.


The var type allows us to omit type from the left-hand side of our statements.

var is a reserved type name, not a keyword,


which means that existing code that uses var as a variable, method, or package name is not a ected.
However, code that uses var as a class or interface name is a ected and the class or interface needs to be renamed.

© 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

You might also like