Now that Java 17 has been released, many students are eager to try it out, but are worried that configuring the new JDK will affect the current project environment. Today we introduce a project-level JDK configuration approach. Gives you a quick start on Java 17 without affecting the original project.

The project quickly integrates Java 17

Amazon’s Corretto JDK 17 and Zulu JDK 17 have both been added to the deluxe lunch.

Then you can download the JDK for Java 17. Maybe it was just released. It was so slow. So I went directly to JDK17 and downloaded a copy. Unzip to Windows current user folder (my folder is C:\Users\ n1. JDKS), the reason why unzip to. JDKS is because IDEA download target folder is this folder, convenient IDEA automatic check out.

There’s no need to reconfigure Java environment variables, it’s all project-level Java versioning, and it won’t affect your other projects.

Then create a New Maven project (which can also be a regular project or Gradle project) before you can have fun. You need to be sure of two things.

Language level

Change the JDK Language Level to Java 17, press Ctrl+Alt+Shift+S under IDEA to exhale the dialog box below and change the Language Level to 17.

Bytecode version

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

Record Class

With the environment configured, we started to try one of the most intuitive and quite useful syntactic sugar records.

This is not exactly a new feature in Java 17, but was first introduced in Java 14 and made official in Java 16. But it’s still an important concept for the LTS version.

Just to be intuitive, the traditional way to write 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(a) {
        return username;
    }

    public Integer age(a) {
        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(a) {
        return Objects.hash(username, age);
    }

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

Using Record can be simplified as:

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

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

Record is immutable

Record is designed to transmit immutable data. As you can see from the above example, the properties of a Record class cannot be changed after it is initialized. There is no Setter method but to initialize the data through a full parameter construction, which is naturally thread safe.

Record of the superclass

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

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

You can also see that all Record implementations overwrite equals, hashCode, and toString methods.

How do YOU determine if a class is a Record class?

Traditional methods:

Record.class.isAssignableFrom(MyRecord.class)
Copy the code

The JDK provides a new approach to this problem:

MyRecord.class.isRecord()
Copy the code

It is worth noting that the Class Class also provides getRecordComponents to get the Record Class member attribute information.

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

Record cannot use the extends keyword

Because the only implicit superclass of the Record class is java.lang.Record, Java does not support multiple inheritance, and using the extends explicit definition can cause compilation errors.

Unable to define additional member variables

Member variables of the Record class can only be declared by construction. So the following is incorrect:

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

But you can define static variables in the Record class.

You need to be careful when defining methods

Defining methods is more open, but make sure that the methods you define don’t undermine the immutable meaning of Record. Defining Setter methods is not recommended.

Also note that the getters for the Record class are not in setXXXX format.

Using annotations

The only thing to watch out for is the Getter methods that might be used by using annotations on member variables of the Record class. Something like this:

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

The compiled:

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

The scope depends on the scope of the @target meta-annotation on the annotation.

conclusion

Today I showed you how to integrate Java 17 quickly without affecting existing projects. Taking this opportunity to introduce and explain the Record class, I hope to help you when you first contact this new definition. Original is not easy, but also please pay more attention to, like, look again, forward.

Follow our public id: Felordcn for more information

Personal blog: https://felord.cn