woshikid / blog

Apache License 2.0
8 stars 1 forks source link

Java16新特性 #160

Open woshikid opened 3 years ago

woshikid commented 3 years ago

与Maven默认插件兼容问题

Cannot access defaults field of Properties Could not initialize class org.apache.maven.plugin.war.util.WebappStructureSerializer

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-war-plugin</artifactId>
    <version>3.3.1</version>
</plugin>

instanceof模式匹配

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

限制使用JDK内部类

默认--illegal-access=deny

垃圾收集器

ZGC收集器支持多线程并发处理 更及时的Metaspace空间释放

Stream API

Stream.of(1, 2, 3).toList();