Java 14 New Features | What is Record in Java 14 ? Explain | Example Java Code Demo | InterviewDOT

preview_player
Показать описание

Tamil Java 14 New Features | Explain Java 14 Records | Example Java Code Demo | InterviewDOT

Motivation and Goals
It is a common complaint that "Java is too verbose" or has too much "ceremony". Some of the worst offenders are classes that are nothing more than plain "data carriers" that serve as simple aggregates. To write a data carrier class properly, one has to write a lot of low-value, repetitive, error-prone code: constructors, accessors, equals(), hashCode(), toString(), etc. Developers are sometimes tempted to cut corners such as omitting these important methods (leading to surprising behavior or poor debuggability), or pressing an alternate but not entirely appropriate class into service (because it has the "right shape" and they don't want to declare yet another class).

IDEs will help write most of the code in a data carrier class, but don't do anything to help the reader distill the design intent of "I'm a data carrier for x, y, and z" from the dozens of lines of boilerplate. Writing Java code that models simple aggregates should be easier -- to write, to read, and to verify as correct.

While it is superficially tempting to treat records as primarily being about boilerplate reduction, we instead choose a more semantic goal: modeling data as data. (If the semantics are right, the boilerplate will take care of itself.) It should be easy, clear, and concise to declare shallowly-immutable, well-behaved nominal data aggregates.
Description
Records are a new kind of type declaration in the Java language. Like an enum, a record is a restricted form of class. It declares its representation, and commits to an API that matches that representation. Records give up a freedom that classes usually enjoy: the ability to decouple API from representation. In return, records gain a significant degree of concision.

A record has a name and a state description. The state description declares the components of the record. Optionally, a record has a body. For example:

record Point(int x, int y) { }
Because records make the semantic claim of being simple, transparent holders for their data, a record acquires many standard members automatically:

A private final field for each component of the state description;
A public read accessor method for each component of the state description, with the same name and type as the component;
A public constructor, whose signature is the same as the state description, which initializes each field from the corresponding argument;
Implementations of equals and hashCode that say two records are equal if they are of the same type and contain the same state; and
An implementation of toString that includes the string representation of all the record components, with their names.
In other words, the representation of a record is derived mechanically and completely from the state description, as are the protocols for construction, deconstruction (accessors initially, and deconstruction patterns when we have pattern matching), equality, and display.

You've for sure already heard this statement before, from your colleagues, at a conference, probably in meetups, Java Language Architect at Oracle, wrote a detailled post in that matter, stating that, as example, developers who want to create simple data carrier classes in a way that are easy to understand have to write a lot of low-value, repetitive, error-prone code: constructors, accessors, equals(), hashCode(), toString()...
To avoid the frustration, some make use of IDE capabilities to do the legwork of writing the boilerplate, but fail to consider much beyond functionality of the code itself to help the reader distill the design intent. Others use some libraries such as Lombok, While the lazy ones just omit the those methods, leading to surprising behavior and poor debuggability.

A new type declaration: Record!
Records are a special kind of lightweight classes in java, intended to be simple data carriers, similar to what exist in other languages (such as case classes in Scala, data classes in Kotlin and record classes in C#). The aim is to extend the Java language syntax and create a way to say that the type represents only data. By making this statement, We're telling the compiler to do all the work for us and produce the methods without any effort from outside.

The record class is an immutable, transparent carrier for a fixed set of fields known as the record components that provides a state description for the record. Each component gives rise to a final field that holds the provided value and an accessor method to retrieve the value. The field name and the accessor name match the name of the component.
Комментарии
Автор

Good to view latest verison with simple explanation

MsGayathri
Автор

it’s look like lambok library anyway finally java 14 added this feature much waited and thanks for explanation

venkateswaran
visit shbcf.ru