Java14: Records – DZone Java | xxxJava14: Records – DZone Java – xxx
菜单

Java14: Records – DZone Java

一月 31, 2020 - MorningStar

Over a million developers have joined DZone.

{{announcement.body}}

{{announcement.title}}

Let’s be friends:

1024)” dc-slot=”ads.sl1.slot(articles[0], 0)” tags=”ads.sl1.tags(articles[0], 0)” size=”ads.sl1.size(articles[0], 0)” style=”border:0px;”>
1 && !articles[0].partner.isSponsoringArticle && (width > 1024)” dc-slot=”ads.sb2.slot(articles[0], 0)” tags=”ads.sb2.tags(articles[0], 0)” size=”ads.sb2.size(articles[0], 0)”>

Java14: Records

DZone ‘s Guide to

Java14: Records

In this article, check out a new feature of Java 14, Records, and see how you can implement them as more effective "data carriers".

Java14: Records - DZone Java by

CORE ·

Mar. 11, 20 · Java Zone ·

Free Resource

Join the DZone community and get the full member experience.

Join For Free

Java 14 introduces a new interesting feature: records. Here, you can find the official proposal: JEP 359 Records. The goal is to have a "data carrier" class without the traditional ‘Java ceremony’ (boilerplate).

In other words, a record represents an immutable state. Record, like Enum, is a restricted form of class. The feature has still a preview status and it could change in future releases.

Use Cases

Typical use cases are: DTOs, compound keys, data structures used as tuples (e.g. Pair, Map.Entry), method’s multiple return, tree nodes.

Records are not intended to replace (mutable) data objects or libraries like Lombok.

Benefits

  • equals(), hashCode(), toString(),  constructor(), and read accessors are generated for you.
  • Interfaces can be implemented.

Restrictions

  • A record cannot be extended — it’s a final class.
  • A record cannot extend a class.
  • The value (reference) of a field is final and cannot be changed.

Record Definition

Java14: Records - DZone Java

The body is optional. The state description declares the components of the record. This simple line of code is translated by the compiler in a class similar to this one:

Java

 

x

21

 

1

public final class Person extends Record {

2

  public final String name;

3

  public final Integer yearOfBirth;

4

 

5

  public Person(String name, Integer yearOfBirth) {

6

    this.name = name;

7

    this.yearOfBirth = yearOfBirth;

8

  }

9

  

10

  public String name() {

11

  public final String name;

0

12

  public final String name;

1

13

  public final String name;

2

14

  public final String name;

3

15

  public final String name;

4

16

  public final String name;

5

17

  public final String name;

6

18

  public final String name;

7

19

  public final String name;

8

20

  public final String name;

9

21

  public final Integer yearOfBirth;

0

Beware

If the fields contain objects, only the reference is immutable. The referenced objects can change its value, compromising the state of the record. For this reason, you should use immutable objects in your record to avoid surprises.

You may also like: A Guide to Streams: In-Depth Tutorial With Examples.

Examples

How to Execute Them

To execute the examples, you can use JShell with the flag --enable-preview or compile your source using the flags javac --enable-preview --release 14 [source].java and execute it using java --enable-preview [mainclass].

If you are using a single file program, you need the source flag: java --enable-preview --source 14 [source].java

The code in this post has been tested with JShell and IntelliJ (EAP), using OpenJDK build 14-ea+32-1423.

minimalistic

Java

 

x
 

1

  public final Integer yearOfBirth;

1

This is a minimalistic valid record.

Java

 

  public final Integer yearOfBirth;

2

1

 

1

  public final Integer yearOfBirth;

3

2

  public final Integer yearOfBirth;

4

3

  public final Integer yearOfBirth;

5

4

  public final Integer yearOfBirth;

6

5

  public final Integer yearOfBirth;

7

6

  public final Integer yearOfBirth;

8

7

  public final Integer yearOfBirth;

9

8

 

0

9

 

1

Adding a Field

In this example, we add an argument to the new record.

Java

 

 

2

1

 

1

 

3

Java adds the private field ( final String name;) and the accessor ( public String name() {return this.name;}) to the class and implements toString(), equals(), and the constructor Person(String name) {this.name = name}.

Java14: Records - DZone Java

Record workflow

Java

 

 

4

1

17

 

1

 

5

2

 

6

3

 

7

4

 

8

5

 

9

6

  public Person(String name, Integer yearOfBirth) {

0

7

  public Person(String name, Integer yearOfBirth) {

1

8

  public Person(String name, Integer yearOfBirth) {

2

9

  public Person(String name, Integer yearOfBirth) {

3

10

  public Person(String name, Integer yearOfBirth) {

4

11

  public Person(String name, Integer yearOfBirth) {

5

12

  public Person(String name, Integer yearOfBirth) {

6

13

  public Person(String name, Integer yearOfBirth) {

7

14

  public Person(String name, Integer yearOfBirth) {

8

15

  public Person(String name, Integer yearOfBirth) {

9

16

    this.name = name;

0

17

    this.name = name;

1

Noteworthy here:

  • the fields are private and final
  • an accessor is created for the fields without the traditional bean notation ‘get’.

implementing an interface

records can implement an interface, here an example:

Java

 

    this.name = name;

2

1

12

 

1

    this.name = name;

3

2

    this.name = name;

4

3

    this.name = name;

5

4

    this.name = name;

6

5

    this.name = name;

7

6

    this.name = name;

8

7

    this.name = name;

9

8

    this.yearOfBirth = yearOfBirth;

0

9

    this.yearOfBirth = yearOfBirth;

1

10

    this.yearOfBirth = yearOfBirth;

2

11

    this.yearOfBirth = yearOfBirth;

3

12

    this.yearOfBirth = yearOfBirth;

4

Java14: Records - DZone Java

Implementing Multiple Constructors

Records implement a constructor with fields declared as parameters.

Java

 

    this.yearOfBirth = yearOfBirth;

5

1

 

1

    this.yearOfBirth = yearOfBirth;

6

This code generates something like:

Java

 

    this.yearOfBirth = yearOfBirth;

7

1

16

 

1

    this.yearOfBirth = yearOfBirth;

8

2

    this.yearOfBirth = yearOfBirth;

9

3

  }

0

4

  }

1

5

  }

2

6

  }

3

7

  }

4

8

  }

5

9

  }

6

10

  }

7

11

  }

8

12

  }

9

13

  

0

14

  

1

15

  

2

16

  

3

If you try to instantiate the record without the two parameters an exception is thrown:

Java

 

  

4

1

 

1

  

5

2

  

6

3

  

7

4

  

8

5

  

9

6

  public String name() {

0

7

  public String name() {

1

You can add your own constructor if needed (e.g. not all the parameters are required).

Java

 

  public String name() {

2

1

 

1

  public String name() {

3

2

  public String name() {

4

3

  public String name() {

5

4

  public String name() {

6

5

  public String name() {

7

In this case, you can instantiate an object using the extra constructor:

Java

 

  public String name() {

8

1

 

1

  public String name() {

9

Mutating the State

In this example, I show how it’s possible to mutate the values inside a record. The types used in a record should be immutable to be sure that the state won’t change.

Java

 

  public final String name;

00

1

20

 

1

  public final String name;

01

2

  public final String name;

02

3

  public final String name;

03

4

  public final String name;

04

5

  public final String name;

05

6

  public final String name;

06

7

  public final String name;

07

8

  public final String name;

08

9

  public final String name;

09

10

  public final String name;

10

11

  public final String name;

11

12

  public final String name;

12

13

  public final String name;

13

14

  public final String name;

14

15

  public final String name;

15

16

  public final String name;

16

17

  public final String name;

17

18

  public final String name;

18

19

  public final String name;

19

20

  public final String name;

20

Redefining an Accessor

When you declare a record, you can redefine an accessor method. This is useful if you need to add annotations or modify standard behavior.

Java

 

  public final String name;

21

1

10

 

1

  public final String name;

22

2

  public final String name;

23

3

  public final String name;

24

4

  public final String name;

25

5

  public final String name;

26

6

  public final String name;

27

7

  public final String name;

28

8

  public final String name;

29

9

  public final String name;

30

10

  public final String name;

31

Annotations

Records components support annotation. The annotation requires has to declare RECORD_COMPONENT as target.

Java

 

  public final String name;

32

1

 

1

  public final String name;

33

2

  public final String name;

34

3

  public final String name;

35

4

  public final String name;

36

5

  public final String name;

37

6

  public final String name;

38

Further Reading

Topics:
java ,java 14 ,jdk ,openjdk ,records ,java records

Published at DZone with permission of Marco Molteni . See the original article here.

Opinions expressed by DZone contributors are their own.

Java Partner Resources

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.linkDescription }}

{{ parent.urlSource.name }}

· {{ parent.articleDate | date:’MMM. dd, yyyy’ }} {{ parent.linkDate | date:’MMM. dd, yyyy’ }}



Notice: Undefined variable: canUpdate in /var/www/html/wordpress/wp-content/plugins/wp-autopost-pro/wp-autopost-function.php on line 51