Object obj = "String";
if (obj instanceof String s) {
System.out.println(s.length()); // 内部可见
}
if (obj instanceof String s && s.length() > 0) { // 右部可见
}
if (!(obj instanceof String s)) return;
System.out.println(s.length()); // 外部可见
Record类型
public record Point(int x, int y) {}
等同于
public final class Point { // extends Record
private final int x;
private final int y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
public int x() {
return x;
}
public int y() {
return y;
}
@Override
public boolean equals(Object o) {
return o instanceof Point p && p.x == x && p.y == y;
}
@Override
public int hashCode() {
return Objects.hash(x, y);
}
@Override
public String toString() {
return String.format("Point[x=%d, y=%d]", x, y);
}
}
构造函数
public Point {
x = Math.abs(x);
y = Math.abs(y);
}
等同于
public Point(int x, int y) {
x = Math.abs(x);
y = Math.abs(y);
this.x = x;
this.y = y;
}
与Maven默认插件兼容问题
Cannot access defaults field of Properties Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer
instanceof模式匹配
Record类型
等同于
构造函数
等同于
限制使用JDK内部类
默认
--illegal-access=deny
垃圾收集器
ZGC收集器支持多线程并发处理 更及时的Metaspace空间释放
Stream API