
本文介绍了JEP 359中定义的Java 14新功能–记录或数据类。
PS此record
数据类是预览功能
为什么要记录?
Java太冗长,如果要声明一个class,
则需要创建许多繁琐且重复的方法,例如构造函数,访问器, equals()
, hashCode()
和toString()
。 最后,Java 14引入了record
类,以通过自动创建所有繁琐的方法来简化过程。
record
数据类。
public record Point(int x, int y){};
1.反编译Java 14记录类。
尝试反编译上面的Point.class
,我们将获得以下源代码。

// IntelliJ API Decompiler stub source generated from a class file
// Implementation of methods is not available
package com.mkyong.java14.jep359;
public final class Point extends java.lang.Record {
private final int x;
private final int y;
public Point(int x, int y) { /* compiled code */ }
public java.lang.String toString() { /* compiled code */ }
public final int hashCode() { /* compiled code */ }
public final boolean equals(java.lang.Object o) { /* compiled code */ }
public int x() { /* compiled code */ }
public int y() { /* compiled code */ }
}
Java 14 record
将自动生成以下变量和方法。
- 最后的类扩展了
java.lang.Record
。 - 私有最终实例变量。
- 构造函数,(以实例变量作为参数)
-
toString()
-
hashCode()
和equals()
,用于对象比较。 - 实例变量的
getter()
, 无setter的所有最终变量。

很好,它为我们节省了在IDE中的几次单击。
2.如何使用Java 14 Record?
本示例向您展示如何使用record
类。
public record Point(int x, int y){};
package com.mkyong.java14.jep359;
public class PointApp {
public static void main(String[] args) {
// constructor Point(int x, int y)
Point p1 = new Point(10, 20);
// getters int x()
System.out.println(p1.x()); // 10
// getters int y()
System.out.println(p1.y()); // 20
Point p2 = new Point(11, 22);
System.out.println(p2.x()); // 11
System.out.println(p2.y()); // 22
Point p3 = new Point(10, 20);
System.out.println(p3.x()); // 10
System.out.println(p3.y()); // 20
// hashCode and equals
System.out.println(p1.hashCode()); // 330
System.out.println(p2.hashCode()); // 363
System.out.println(p3.hashCode()); // 330
System.out.println(p1.equals(p2)); // false
System.out.println(p1.equals(p3)); // true
System.out.println(p1.equals(p1)); // true
// toString()
System.out.println(p1); // Point[x=10, y=20]
System.out.println(p2); // Point[x=11, y=22]
System.out.println(p3); // Point[x=10, y=20]
}
}
输出量
10
20
11
22
10
20
330
363
330
false
true
true
Point[x=10, y=20]
Point[x=11, y=22]
Point[x=10, y=20]
Java开发人员的生活现在非常简单simple
3.覆盖默认的Record方法。
3.1查看Location
记录类,我们将覆盖默认记录的构造函数和toString()
。
package com.mkyong.java14.jep359;
public record Location(double latitude, double longitude) {
// override record default constructor
public Location {
this.latitude = latitude * 3;
this.longitude = longitude * 3;
}
// override record toString
@Override
public String toString() {
return "GPS Location{" +
"latitude=" + latitude +
", longitude=" + longitude +
'}';
}
}
3.2测试。
package com.mkyong.java14.jep359;
public class LocationApp {
public static void main(String[] args) {
Location loc = new Location(10, 20);
System.out.println(loc);
}
}
输出量
GPS Location{latitude=30.0, longitude=60.0}
4.记录可序列化吗?
我们可以将record
类读取或写入文件吗? 答案是肯定的,它使record
类实现了Serializable
并且可以工作。
package com.mkyong.java14.jep359;
import java.io.*;
record GPS(double latitude, double longitude) implements Serializable {
};
public class Java14FileApp {
private static final String FILE_PATH = "location.obj";
public static void main(String[] args) {
GPS obj = new GPS(10, 20);
save(obj, FILE_PATH);
GPS result = read(FILE_PATH);
System.out.println(result);
}
private static void save(GPS obj, String path) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path))) {
oos.writeObject(obj);
} catch (IOException e) {
e.printStackTrace();
}
}
private static GPS read(String path) {
GPS result = null;
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(path))) {
result = (GPS) ois.readObject();
} catch (ClassNotFoundException | IOException e) {
e.printStackTrace();
}
return result;
}
}
输出量
GPS[latitude=10.0, longitude=20.0]
5.预览语言功能
5.1该record
类是预览功能。 我们需要使用--enable-preview
选项来手动启用它。
$ javac --enable-preview --release 14 Point.java
$ java --enable-preview Point
5.2对于IntelliJ IDE,请更新到最新版本2020.1.1;否则,请重新安装。 它应该支持Java 14的新预览功能。

下载源代码
$ git clone https://github.com/mkyong/core-java
$ cd java-14
参考文献
From: https://mkyong.com/java/java-14-record-data-class/