Keeping up with the six-month cycle tradition, after the release of Java 14 on March 17, 2020, we now have Java 15, the next non-LTS version rolling out on September 15, 2020.
Here’s a quick look at features that are a part of Java 15:
/Library/Java/JavaVirtualMachines
as shown below:$ cd /Library/Java/JavaVirtualMachines
$ sudo cp ~/Downloads/openjdk-15_osx-x64_bin.tar.gz /Library/Java/JavaVirtualMachines
$ sudo tar xzf openjdk-15_osx-x64_bin.tar.gz
$ sudo rm openjdk-15_osx-x64_bin.tar.gz
Once that’s done, open the bash_profile
using any text editor. I’m using vim ~/.bash_profile
. Set the path of Java15 to JAVA_HOME, save changes and do a source ~/.bash_profile
to reflect the changes.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-15.jdk/Contents/Home
Finally, you’re ready to compile and run programs using Java 15. We’ll be using JShell, an interactive REPL command-line tool for quickly testing the new Java 15 features.
It’s important to note that many features released in Java 15 are in preview. This means that though they’re fully working right now, things may be modified in the future. Some could be made a standard or simply removed in the next release cycles. In order to test preview features, you need to explicitly set --enable-preview
when running the JShell or Java Program as shown below:
jshell --enable-preview
javac --release 15 --enable-preview Author.java
In the next few sections, let’s discuss the significant language changes in Java 15.
Sealed classes have been there in Kotlin since a while and Java 15 finally introduces this feature for better control over inheritance.
As the name suggests, Sealed classes let you restrict or permit class hierarchies to only certain types.
This is incredibly useful for pattern matching as you have a specific number of classes to switch
between.
The following syntax defines a sealed class in Java 15:
public sealed class Vehicle permits Car, Bike, Truck {
...
}
So, the above code means, only the classes defined after the keyword permits
are allowed to extend the Vehicle
sealed
class.
In case, you’ve defined the classes Car
, Bike
and Truck
in the same file as Vehicle
, you can omit the keyword permits and the compiler will take care of it implicitly as shown below:
sealed class Vehicle {...}
final class Car extends Vehicle {...}
final class Bike extends Vehicle {...}
final class Truck extends Vehicle {...}
As you can see above, we’ve defined the final modifier of each of the classes. Now, that’s an important rule of sealed class that you need to keep in mind: Every permitted class must be set with an explicit modifier. It can either be final
or sealed
or non-sealed
.
Here’s how each of the modifiers impact inheritance:
final
cannot be extended further.sealed
can be extended further but only by classes that are permitted by the subclass.non-sealed
can be extended further by any class. The superclass cannot restrict the subclasses further down this class hierarchy.Before Java 15, developers could only use final keyword or scope modifiers to control inheritance. So, sealed classes brings the added flexibility for Java developers when defining class hierarchies.
Java’s Reflection API also gets two new methods for dealing with sealed classes:
java.lang.constant.ClassDesc[] getPermittedSubclasses();
boolean isSealed()
Records were introduced as a preview feature in Java 14, in an effort to reduce boilerplate code when writing POJO based data carrier classes. This is something that’s been there in Kotlin for long in the form of data classes.
Now, with Java 15, Records get their second preview. While there aren’t any major changes(just some minor additions), still there are a few major clarifications and restrictions that you should know:
native
method steals away the USP of records by bringing an external state dependency.final
and should not be modified via reflection now as it will throw IllegalAccessException
.Records are meant to be data carrier classes and you should totally avoid defining native methods in them.
We know that records are final and cannot be extended. Gladly, records can implement interfaces.
So you can define a sealed interface and implement them on your records in the following ways:
sealed interface Car permits BMW, Audi { ... }
record BMW(int price) implements Car { ... }
record Audi(int price, String model) implements Car { ... }
Records can also be defined within methods to store intermediate values. Unlike local classes, a local record is implicitly static. This means they cannot access variables and instance members of the enclosing methods which is actually great since it prevents capturing of values by the record.
Local records are a great boon for Java developers who would earlier have to create helper records.
See how the introduction of a local records helps perform computation of values in a stream API with the following method:
List<Merchant> findTopMerchants(List<Merchant> merchants, int month) {
// Local record
record MerchantSales(Merchant merchant, double sales) {}
return merchants.stream()
.map(merchant -> new MerchantSales(merchant, computeSales(merchant, month)))
.sorted((m1, m2) -> Double.compare(m2.sales(), m1.sales()))
.map(MerchantSales::merchant)
.collect(toList());
}
While the above two were the two major language features in Java 15, we also have Pattern Matching in the second preview for user feedback, Text Blocks as a standard feature now and most importantly a new Hidden classes feature.
Hidden classes are a JVM feature which is relevant for framework developers. It allows class implementations to be made non-discoverable by defining them using Lookup::defineHiddenClass
. In doing so, such classes can neither be found using Class.forName
nor by referring them in bytecode.
These are the major changes introduced in Java 15.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Sign up for Infrastructure as a Newsletter.
Working on improving health and education, reducing inequality, and spurring economic growth? We'd like to help.
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
it will be a good time to really have time with you
- john
Java 16 features Please
- Dhivya
Thanks for Java 15 new features :)
- Neeraj Kumar