What’s New in Java 15
Sealed classes are the new concept that was introduced by Java 15. Sealed classes are a preview feature. Most of the features which are released in java 15 are in preview. We have to note it out. Even though the new features are working fine and properly now, they may get modified in future. There is a chance of getting some features that may become standard, and some features may get removed. We can test preview features; all we need to do is that we have explicitly set "-enable-preview" when running the Java program or JShell, as shown.
shell –enable-preview
javac –release 15 –enable-preview Author.java
Pattern Matching, for instance (Second Preview) – JEP 375, Records (Second Preview) – JEP 384, Text Blocks (Standard)., Hidden Classes – JEP 371, Disable and Deprecate Biased Locking – JEP 374, Reimplement the Legacy Datasocket API – JEP 373, Foreign – Memory Access API (Second Incubator) – JEP 383, Remove the Solaris and SPARC Ports – JEP 381, A Low Pause Time Garbage Collector – JEP 379, Remove the Nashron Javascript Engine – JEP 372 are the some of the few new features in Java 15.
Records – JEP 384
Actually, these records were originally introduced in java 14. By using these records, we can easily create immutable data objects. These records are the new type of classes in java 15.
Let’s have a look at how they will be the program with and without records.
Without Records:
Let us take a program with no use records.
public class Animal {
private final String animal_name;
private final int animal_age;
public Animal (String animal_name, int animal_age) {
this.animal_name = animal_name;
this.animal_age = animal_age;
}
public String getname() {
return animal_name;
}
public int getAge() {
return animal_age;
}
}
In the program, we created an immutable data transfer object prior to records.
If we observe the program that we have written, we can notice that we have written a lot of code for creating an immutable object. By using the final, we explicitly defined all of our fields. Also, we can notice that we have an accessor method for every field which we have written. We have all single all-arguments constructor.
We also override the toString method in the code we have written to provide meaningful logging output. To avoid unexpected consequences, we also want to override the hashCode and equals methods when we compare the two instances of these objects.
With Records:
Here we are defining an immutable data object by using the records in a compact way of the same program.
public record Person (String animai_name, int animal_age) {
}
We observe the syntax of records. The syntax is the class definition has a new syntax that is only specific for records. And the header contains the details about the fields inside the record.
Let us take a program using records.
public record Animal(String animal_name, int animal_age) {
public Animal {
if( animal_age < 0) {
throw new IllegalException(“ Age cannot be negative for any creature in this world”);
}
}
}
Boilerplate code can mostly be eliminated by using records in the program. By using records, we can override some of the default behaviours, and this feature is allowed by records.
When we use records, the compiler provides sensible implementations for the equals, for toString methods and also for hashCode methods when we write the program using records compared to the program written without using records. Here we can observe the code which we have written. We do not use the final keyword in it because they are always final. Records do have some restrictions. We cannot use records as native methods, and also they cannot be declared as abstract.
Sealed Classes – JEP 360
In the syntax of sealed classes, we have two keywords, permits and sealed. And also, we use abstract keywords.
Let us look at the example syntax of sealed classes to understand the concept of sealed classes.
Syntax to understand how the Sealed classes work:
public abstract sealed class Human
permits Employee_name, Manger_id {
// ......
}
In the above, we declared an abstract class named Human. Also, we have specified that the only classes can extend it are Employee_name and Manager_id.
Extending sealed classes is similar to extend keyword that we are using in the current java version.
Public final class Employee_name extends Human {
}
Public non-sealed class Manger_id extends Human {
}