Java 简明教程

Java - Record

在 Java 14 中,引入了一个令人兴奋的功能 {} 作为预览功能。{} 功能有助于创建不可变数据对象。在 Java 15 版本中,{} 得到进一步增强。在 Java 14 和 15 中,为了使用 {},必须传递一个标志 --enable-preview。从 Java 16 开始,此标志不再需要,因为 {} 是 JDK 的标准部分。

In Java 14, an exciting feature record was introduced as a preview feature. The record feature helps in creating immutable data objects. In the Java 15 version, record types were enhanced further. In Java 14 and 15, in order to use a record, a flag --enable-preview has to be passed. From Java 16 onwards, this flag is not required as the record is a standard part of JDK.

Purpose of a Java Record

记录的主要目的是创建一个数据对象或 POJO,用于在应用程序流程中携带数据。在多层程序中,域/模型对象存储从数据源捕获的数据,然后将这些模型对象进一步传递到应用程序/UI 层以处理数据,反之亦然,其中 UI/应用程序将数据存储在数据对象中,然后将这些对象传递给数据层以填充数据。源。

The prime purpose of a record is to create a data object or a POJO which is used to carry data in application program flow. In a multi-tier program, Domain/Model objects store the data captured from the data source and then these model objects are passed further to the application/UI layer to process the data and vice versa where UI/Application stores data in data objects and then pass these objects to Data layer to populate data sources.

由于这些数据对象包含许多字段,因此要求开发人员编写许多 setter/getter 方法、参数化 constructors、重写 equals 方法和 hashcode 方法。在这样的场景中,record 提供了帮助,因为它提供了大部分样板代码,而开发人员只需关注必需的功能。

As these data objects contain a lot of fields, developers are required to write a lot of setter/getter methods, parameterized constructors, overridden equals methods, and hashcode methods. In such a scenario, the record comes to the rescue as it provides most of the boilerplate code and the developer can focus on required functionalities only.

Features of Java Record

以下是使记录成为令人兴奋的功能的记录功能:

Following are the features of the record which makes the record an exciting feature:

  1. Record objects have an implicit constructor with all the parameters as field variables.

  2. Record objects have implicit field-getter methods for each field variable.

  3. Record objects have implicit field setter methods for each field variable.

  4. Record objects have an implicit sensible implementation of hashCode(), equals(), and toString() methods.

  5. With Java 15, native methods cannot be declared in records.

  6. With Java 15, implicit fields of record are not final and modification using reflection will throw IllegalAccessException.

Example Without Using Java Record

让我们创建一个不使用记录的简单程序,其中我们要创建一个 Student 对象并打印其详细信息。该 Student 有三个属性:id、name 和 className。为了创建学生,我们创建了一个带参数的构造函数、设置器、访问器方法、equals 和散列码方法。因此,我们的 Student 类用近 60 多行代码就完成了。

Let’s create a simple program without using record where we’re creating a Student object and printing its details. The Student has three properties, id, name and className. In order to create a student, we’ve create a parameterized constructor, setter, getter methods, equals and hashcode methods. Thus our Student class is completed with nearly 60+ lines.

package com.tutorialspoint;

public class Tester {
   public static void main(String args[]) {
      // create student objects
      Student student1 = new Student(1, "Mahesh", "XII");
      Student student2 = new Student(2, "Sudhir", "XII");

      // print the students
      System.out.println(student1);
      System.out.println(student2);

      // check if students are same
      boolean result = student1.equals(student2);
      System.out.println(result);

      // check if students are same
      result = student1.equals(student1);
      System.out.println(result);

      // get the hashcode
      System.out.println(student1.hashCode());
      System.out.println(student2.hashCode());
   }
}

class Student{
   private int id;
   private String name;
   private String className;

   Student(int id, String name, String className){
      this.id = id;
      this.name = name;
      this.className = className;
   }

   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public String getName() {
      return name;
   }
   public void setName(String name) {
      this.name = name;
   }
   public String getClassName() {
      return className;
   }
   public void setClassName(String className) {
      this.className = className;
   }

   @Override
   public String toString() {
      return "Student[id: " + id + ", name: " + name
         + ", class: " + className + "]";
   }

   @Override
   public boolean equals(Object obj) {
      if(obj == null || !(obj instanceof Student) ) {
         return false;
      }
      Student s = (Student)obj;

      return this.name.equals(s.name)
         && this.id == s.id
         && this.className.equals(s.className);
   }

   @Override
   public int hashCode() {
      int prime = 19;
      int result = 1;
      result = prime * result + ((name == null) ? 0 : name.hashCode());
      result = prime * result + ((className == null) ? 0 : className.hashCode());
      result = prime * result + id;
      return result;
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Student[id: 1, name: Mahesh, class: XII]
Student[id: 2, name: Sudhir, class: XII]
false
true
371251946
288252156

Example Using Java Record

让我们使用 record 重新创建上面的程序,其中我们将一个 Student 对象创建为 record 并打印其详细信息。该 Student 有三个属性:id、name 和 className。这里您可以看到区别,完整的 Student 类被一行代码替换为 Student record。

Let’s recreate above program using record where we’re creating a Student object as record and printing its details. The Student has three properties, id, name and className. Here you can see the difference, the complete Student class is replaced with one line of code as Student record.

package com.tutorialspoint;

public class Tester {
   public static void main(String args[]) {
      // create student objects
      Student student1 = new Student(1, "Mahesh", "XII");
      Student student2 = new Student(2, "Sudhir", "XII");

      // print the students
      System.out.println(student1);
      System.out.println(student2);

      // check if students are same
      boolean result = student1.equals(student2);
      System.out.println(result);

      // check if students are same
      result = student1.equals(student1);
      System.out.println(result);

      // get the hashcode
      System.out.println(student1.hashCode());
      System.out.println(student2.hashCode());
   }
}

record Student(int id, String name, String className) {}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Student[id: 1, name: Mahesh, class: XII]
Student[id: 2, name: Sudhir, class: XII]
false
true
371251946
288252156

我们还可以向记录中添加自定义方法。但一般不需要。

We can add custom methods in records as well. But generally it is not required.

Java Record for Sealed Interfaces

由于记录在默认情况下为最终记录,并且可以扩展 interfaces。我们可以定义 sealed interfaces,让记录实现它们以便更好地进行代码管理。

As records are final by default and can extend interfaces. We can define sealed interfaces and let records implement them for better code management.

Example: Use of Java Record for Sealed Interfaces

考虑以下示例 −

Consider the following example −

package com.tutorialspoint;

public class Tester {
   public static void main(String[] args) {
      Person employee = new Employee(23, "Robert");
      System.out.println(employee.id());
	  System.out.println(employee.name());
   }
}
sealed interface Person permits Employee, Manager {
   int id();
   String name();
}
record Employee(int id, String name) implements Person {}
record Manager(int id, String name) implements Person {}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

23
Robert

Overriding Methods of Java Records

我们可以轻松地覆盖记录方法实现并提供我们自己的实现。

We can override a record method implementation easily and provide our own implementation.

Example: Override Java Record Methods

考虑以下示例 −

Consider the following example −

package com.tutorialspoint;

public class Tester {
   public static void main(String args[]) {
      // create student objects
      Student student = new Student(1, "Mahesh", "XII");

      System.out.println(student);
   }
}

record Student(int id, String name, String className) {
   public String toString() {
      return "Id: " + id + ", Name: " + name + ", class: " + className ;
   }
}

让我们编译并运行上述程序,这将生成以下结果 −

Let us compile and run the above program, this will produce the following result −

Id: 1, Name: Mahesh, class: XII