Previously, we used the class
keyword to define classes, but starting from Java 16, we will add one more keyword record
, which can also be used to define classes. The introduction of record
keywords is mainly to provide a more concise and compact way of defining final
classes.
Let's take a closer look at the details of the record class. Companion video tutorial: New in Java 16: Declaring classes with record
declare the record class
The basic syntax for declaring a record class:
record range(int start, int end){}
We know that the class class can live in a separate file, or it can be declared in other classes. Then the record class is the same, it has the following declaration methods:
- A separate document states:
public record range(int start, int end){}
- Declare inside the class:
public class DidispaceTest {
public record range(int start, int end){}
}
- Declaration inside the function:
public class DidispaceTest {
public void test() {
public record range(int start, int end){}
}
}
Detailed explanation of record class
After knowing how to declare it, you will definitely want to use record
to declare only a few elements, what capabilities will it have?
Because record
keyword declaration class is mainly to simplify the declaration of some classes, so it is essentially a special class, or the class of a certain template.
record
The declared class has these characteristics:
- It is a
final
class - Automatically implement
equals
,hashCode
,toString
functions - Member variables are all
public
attributes
So, for the range class written earlier, it is equivalent to a class like this:
public final class range{
final int start;
final int end;
public range(int start, int end) {
this.start = start;
this.end = end;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
range range = (range) o;
return start == range.start && end == range.end;
}
@Override
public int hashCode() {
return Objects.hash(start, end);
}
@Override
public String toString() {
return "range{" +
"start=" + x +
", end=" + y +
'}';
}
public int start(){
return start;
}
public int end(){
return end;
}
}
Because some functions are hidden, we can't see them when the range is defined, so we can try to write a test code like this to verify:
@Test
public void test() {
range r = new range(100, 200);
log.info(r.toString());
range r2 = new range(100, 200);
log.info(String.valueOf(r.equals(r2)));
}
define member function
Because record
the essence of the declaration is also a class, so it is definitely possible to define member functions.
For example, we can define member functions in the record
class like this:
record range(int start, int end){
int distance(){
return end - start;
}
}
Then, you can call it like this:
range r = new range(100, 200);
r.distance();
If you encounter difficulties in the learning process? You can join our high-quality technical exchange group , participate in exchanges and discussions, and learn and progress better! Also, don't go away, follow me, and keep updating the new Java feature column !
Welcome to my public account: Programmer DD. Learn about cutting-edge industry news for the first time, share in-depth technical dry goods, and obtain high-quality learning resources
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。