×

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 {
} 

Related Topics

New Features of Java 14

 Features like Switch expressions and text blocks which are previewed in Java 13 version are standardized in Java 14. Features in Java 14 Switch ExpressionText BlocksRecordsNull Pointer ExceptionsInstance ofPackaging ToolGarbage collectors 1.Switch Expressions Switch...

3 minutes read.

HttpURLConnection

HttpURLConnection Protocol It is a standard set of rules that allow electronic devices to communicate with each other. The http protocol The http protocol is for data communication, distributing, collaborating, hypermedia information systems, on the World Wide...

5 minutes read.

Alice and Bob Problem Java

Alice and Bob were two friends who liked to play games. Both together found a game, which has the description below. The game begins with an integer num, used to create...

2 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Java FileNotFoundException

FileNotFoundException is another exception class accessible in the java.io bundle. The exemption happens when we attempt to get to that document which isn't accessible in the framework. It is a checked...

5 minutes read.

JRE (Java Runtime Environment)

JRE is an installation package that provides an environment to run the Java program on any Operating System. It does not deal with the development process of any application. It is a part...

2 minutes read.

Arithmetic exception in Java

Exception Handling is one of the most potent ways of handling runtime faults and preserving the application's normal flow. In Java, an exception is an out-of-the-ordinary state, and exceptions are...

3 minutes read.

Java RandomAccessfile

Writing and reading to random access files are done using this class. An array of many bytes is how a random access file operates. By changing the implied file pointer...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Can Abstract Classes have Static Methods in Java

Abstract Class An abstract class in Java is one that explicitly uses the keyword "abstract" in its declaration. There are options for both non-abstract and abstract techniques (method with the body)....

4 minutes read.

Association in Java

In Java, association refers to the link between two classes established by their objects. One-to-one, one-to-many, and many-to-many connections are managed via association. The Association defines the multiplicity between objects...

5 minutes read.

Java Command not found

Java Command not found error is displayed if Java is not installed on the computer or if the command prompt cannot find Java.exe to run the program. Our Java software...

3 minutes read.

How to initialize string array in Java

In this tutorial, we will learn how the string array is initialized in java. The initialization of string array in the java by using the NEW (it creates the object for...

3 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

How to Convert Decimal to Octal in Java

How to Convert Decimal to Octal in Java There are two methods to convert Decimal to Octal: Using toOcatlString() method Using user-defined logic Using Integer.toOctalString() method The toOctalString() is astaticmethod of the Integer...

2 minutes read.

Java Program to generate binary numbers

A binary tree can create binary numbers ranging from 1 to n. Every node in a tree, the right, and left nodes, has two children, as is common knowledge. The...

3 minutes read.

Sort Dates in Java

The Collection class's sort() method, which is a component of the Comparator mechanism, arranges the data in decreasing order. The Comparator interface can be used to accomplish the goal while...

4 minutes read.