Java 17 has now been released, and many students are eager to try, but they are worried that configuring a new JDK will affect the current project environment. Today we will introduce a project-level JDK configuration method. Let you get started quickly with Java 17 one step ahead, without affecting the original project.

Project quick integration of Java 17

IDEA 集成 Java 17

Before posting, Amazon's Corretto JDK 17 and Zulu JDK 17 have already joined the luxury lunch.

After choosing, you can download the Java 17 JDK. It may be because it was just released, it is too slow. JDK17 on the official website of Open JDK and downloaded a copy of Decompress it to the Windows current user folder path (mine is C:\Users\n1\.jdks ). The reason why it is decompressed to .jdks is because the download destination folder of IDEA is this folder, which is convenient for IDEA to automatically check it out.

解压完成的效果

There is no need to reconfigure the Java environment variables here. They are all project-level Java version control and will not affect your other projects.

Then create a new Maven project (or a normal project or a Gradle project). At this time, you can't play happily. You need to determine two things.

Language level

Adjust the language level of the JDK to Java 17, press the shortcut key Ctrl+Alt+Shift+S under IDEA to call up the following dialog box and modify the Language Level to 17 .

修改项目 JDK Level

Bytecode version

The bytecode version of the compiler also needs to be adjusted to 17 . In IDEA, press the shortcut key Ctrl+Alt+S to modify the position in the figure.

修改编译器的字节码版本

Record Class

After getting the environment configuration, we started to try one of the most intuitive and useful syntactic sugar Record .

To be precise, this is not a new feature of Java 17. It first appeared in Java 14 and became an official feature in Java 16. But as an LTS version, this is still a very important concept.

Let's be more intuitive. The traditional way of writing a data class is:

public class MyRecord {
    private final String username;
    private final Integer age;

    public MyRecord(String username, Integer age) {
        this.username = username;
        this.age = age;
    }

    public String username() {
        return username;
    }

    public Integer age() {
        return age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyRecord oldRecord = (MyRecord) o;
        return Objects.equals(username, oldRecord.username)
                && Objects.equals(age, oldRecord.age);
    }

    @Override
    public int hashCode() {
        return Objects.hash(username, age);
    }

    @Override
    public String toString() {
        return "MyRecord[" +
                "username='" + username + '\'' +
                ", age=" + age +
                ']';
    }
}

Using Record can be simplified to:

public record MyRecord(String username,Integer age) {
}

This greatly reduces some template code and makes the logic clearer and simpler.

Record is immutable

Record is used to design the transmission of immutable data. As can be seen from the above example, Record class cannot be changed after being initialized. There is no Setter method but the data is initialized through the full-parameter construction, which is natural thread safe.

Superclass of Record

All classes declared with the Record are subclasses of java.lang.Record , which is a bit like an enumeration.

public abstract class Record {
 
    protected Record() {}
 
    @Override
    public abstract boolean equals(Object obj);
 
    @Override
    public abstract int hashCode();
 
    @Override
    public abstract String toString();
}

From here you can see all Record achieved are overwritten equals , hashCode , toString three methods.

How to determine if a class is Record ?

Traditional method:

Record.class.isAssignableFrom(MyRecord.class)

JDK provides a new method to solve this problem:

MyRecord.class.isRecord()

It is worth mentioning that the Class class also provides getRecordComponents to obtain the member attribute information of the Record

RecordComponent[] recordComponents = MyRecord.class.getRecordComponents();

Record cannot use the extends keyword

Because Record class only implicit superclass is java.lang.Record , the Java does not support multiple inheritance, use extends explicitly define causes a compilation error.

Cannot define additional member variables

Record class can only be declared by construction. So the following is wrong:

public record MyRecord(String username,Integer age) {
privite String gender;
}

But you can define static variables Record

Need to be careful when defining methods

The definition method is more open, but please make sure that the method you define does not destroy the Record meaning of 06143f6b197968. not recommended to define the Setter method .

Also note that the Getter method of the Record setXXXX format.

Use annotations

The only thing to note is that using annotations on the member variables of the Record Getter method. like this:

public record MyRecord(@Deprecated String username,Integer age) {
}

After compilation:

public record MyRecord(String username, Integer age) {    public MyRecord(@Deprecated String username, Integer age) {        this.username = username;        this.age = age;    }    public String getUsername() {        return this.username;    }    /** @deprecated */    @Deprecated    public String username() {        return this.username;    }    public Integer age() {        return this.age;    }}

The specific scope needs to be determined according to the definition domain @Target

Summarize

Today I introduced how to quickly integrate Java 17 without affecting existing projects. Taking this opportunity Record category, I hope to help you when you first encounter this new definition. Originality is not easy, please pay attention, like, read again, and forward.

Follow the public account: Felordcn for more information

personal blog: https://felord.cn


码农小胖哥
3.8k 声望8k 粉丝