JDK15 is officially released, preview of new features!

8. The
old wording of instanceof automatic matching mode (preview) :

// first determine the type of
IF (the instanceof String obj) { // then converted String S = (String) obj; // before using } the new wording:




if (obj instanceof String s) { // If the type matches, use it directly } else { // If the type does not match, you cannot use it directly } This is the second preview of this feature, and we have previewed this feature for the first time in Java 14.




9. Records Class (Preview)
Records Class is also the second preview function that appears. It has also appeared in JDK 14. It is more convenient to create a constant class by using Record. The code comparison before and after using is as follows.

Old writing:

class Point {
private final int x;
private final int y;

Point(int x, int y) { 
    this.x = x;
    this.y = y;
}

int x() { return x; }
int y() { return y; }

public boolean equals(Object o) { 
    if (!(o instanceof Point)) return false;
    Point other = (Point) o;
    return other.x == x && other.y = y;
}

public int hashCode() {
    return Objects.hash(x, y);
}

public String toString() { 
    return String.format("Point[x=%d, y=%d]", x, y);
}

}
New writing:

record Point(int x, int y) {}
That is to say, after using the record, you can write a constant class with one line of code, and this constant class also contains the constructor, toString(), equals() and hashCode () and other methods.

10. External memory access API (preview) The
purpose is to introduce an API to allow Java programs to safely and effectively access external memory outside the Java heap. This is also a preview feature of Java 14.
Shenzhen website optimization www.zg886.cn

Guess you like

Origin blog.csdn.net/weixin_45032957/article/details/108636227