Java 15 features.
Major features are consolidated here, Please refer below.
No explicit typecasting in instanceof –
This feature is available in Java 14(preview mode and
java 15 preview). Now in instanceof will implicit typecast the variable into
given instance if instanceOf return true, refer below code snippet –
String str =
(String) obj; // no need to declare and cast again this object in java 15
… str.equals(…)
}else{
} if (obj instanceof String str) {
… str.equals(…)
}else{
}
Records –
To avoid duplicity in class, a new class syntax will be
introduced in mode in Java 15. Using records we can remove lots of duplicate
code regarding constructors, setter, getter, equals(), hashCode(), toString(),
etc
Refer below code –
final class Student {
public final
String firstName;
public final String lastName;
public
PoString(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}
// Above code can re-written using record syntax
Public record student(String firstName, String lastName)
{ }
Note – Records are implicit Final, can’t be abstract or
inherit any other class
Sealed –
Sealed can be applied on class and interface, using the
keyword we restrict the class to inherit and similarly restrict the interface
to implements
Refer below code –
public abstract sealed class Person
permits
Employee, Manager {
}
Here person class can be extended by Employee and
manager.
Hidden class –
The goal of hidden classes is to allow the runtime
creation of classes that are not discoverable. This means they cannot be linked
by other classes, nor can they be discovered via reflection.
Garbage collections –
In Java 15, both ZGC (JEP 377) and Shenandoah (JEP 379)
will be no longer be experimental. Both will be supported configurations that
teams can opt to use, while the G1
collector will remain the default.
No comments:
Post a Comment