A preliminary understanding of the new features in JDK17

1. Enhancement of Switch statement

In jdk12, the switch statement does not need to write break, and directly writes the arrow and the corresponding value.

In jdk 17, a comma is added to match many-to-one.

 If you want to write logic in each case, you can write it in curly braces. Add the yield keyword in front of the return value.

 It is also possible to match on the type

    public static void testPatternMatchSwitch(Object o) {
        switch (o) {
            case null -> System.out.println("首先判断对象是否为空,走空指针逻辑等后续逻辑");
            case String s -> System.out.println("判断是否为字符串,s:" + s);
            case Record p -> System.out.println("判断是否为Record类型: " + p.toString());
            case int[] arr -> System.out.println("判断是否为数组,展示int数组的长度" + arr.length);
            case Integer i -> System.out.println("判断是否为Intger对象,i:" + i);
            default -> System.out.println("Something else");
        }
    }

2. String concatenation

jdk15 provides a text block feature that can directly use three double quotes. 

Two characters are provided in jdk17, \ is used to connect two lines into one line, and \s allows a space to be added before it. 

3. Enhancement of instanceof

Jdk14 preview version, jdk16, jdk17 have enhanced instanceof.

 4. Sealed classes

1. Create an Animal that can only be inherited by Cat and Dog classes.

2. Animal, Cat and Dog classes must all be under the same package.

3. Other classes cannot inherit Animal and Cat classes. Dog can be inherited because it is a non-sealed modification. Note that the Cat and Dog classes can only be final and non-sealed

public abstract sealed class Animal permits Cat, Dog {
}

/**
 * Final 代表 cat 无法再次被子类继承
 */
public final class Cat extends Animal {
}
/**
 * non-sealed 代表可以被再次继承
 */
public non-sealed class Dog extends Animal{
}

Advantages: Safer, controllable and easy to maintain.

5. Record class

Similar to the attribute read-only object in lombok

The record class provides a construction method with full parameters, a get method, and no set method.

public record RecordExample(Long id, String name) {
}

Looking at the bytecode, you will find that, in fact, the java compiler generates a construction method and a get method for us during compilation.

public record RecordExample(Long id, String name) {
    public RecordExample(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long id() {
        return this.id;
    }

    public String name() {
        return this.name;
    }
}

Test the equal method in Record. It will be found that as long as the attributes are the same, the result is the same.

public class TestRecord {

    public static void main(String[] args) {
        RecordExample example1 = new RecordExample(1l, "name1");
        RecordExample example2 = new RecordExample(1l, "name1");

        System.out.println(example1.equals(example2));
        
    }
}

6. Optimized null pointer exception

1), description

The new feature of Java15 is to optimize the log of NullPointerException exception and print it more humanely.

2), case

It can be seen that the prompt will be more directional, which means that you will probably not be troubled by null pointer exceptions in the complex production environment troubleshooting process in the future.
public class TestNPE {

    public static void main(String[] args) {
        User user = new User();
        user.getUserName().toLowerCase();
    }
}

The error message after running can easily locate where it is null.

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because the return value of "com.sealed.example.User.getUserName()" is null
    at com.sealed.example.TestNPE.main(TestNPE.java:8)

6. ZGC Garbage Collector

jdk11 is available, jdk15 has officially become a regular, and jdk17 zgc is already very mature.

The advantage of ZGC is that garbage collection does not freeze. That is to say, there is no STW- stop the world. The time of the card is less than 10ms.

The heap memory can be set very large, even T level.

ZGC high throughput.

ZGC Low Latency

 Very little pause time.

Less extra resource utilization.

GC progress from JDK 8 to JDK 17

Guess you like

Origin blog.csdn.net/keeppractice/article/details/131996834