[MUSIC]
>> Greetings everyone, and welcome back to
Microsoft's Java for Beginners Series.
My name is Brian Benz and I'm a Cloud advocate at Microsoft.
I'm joined by David Grieve,
who's going to talk to us about records.
David, do you want to tell us a little bit
about yourself and about records?
>> Sure. I'm David Grieve.
I work here at Microsoft in the job engineering group,
and I'm here today to talk a little bit about records.
There's been a lot of work that's gone into
Java past few iterations to make it a
more productive language for developers to
use and records is one of those productivity tools.
Records make it very easy to create plain old Java objects,
and that's the topic I'll cover in this first video.
Records. No, not this type of record,
but the record type in Java.
Records were a preview feature in Java
14 and were released in Java 16.
If you have an earlier version of Java,
you'll need to update.
I recommend that you go
to Microsoft.com/openJDK and download
Java 17 from the download button here.
What are records in Java?
A record class is a simple data carrier
that contains data not meant to be altered
and has only the most fundamental methods
such as constructors and accessors.
Data transfer objects are
a good example where records might be used.
Before records came along,
developers would have to code what are known as
plain old Java objects or POJOs.
The problem is that this takes time and
a lot of code to write a POJO.
Most of that is code is pretty much boilerplate.
IDs can generate that code for you,
or you could even use annotations.
But what gets lost is
the intent that this class is just a data carrier.
Really records are not
about reducing the amount of boilerplate code,
but rather the intent of
the Java architects was to model data as data.
The record type is a clear and concise way of saying
this class is a simple data aggregate.
Let's take a look at what that means.
We're going to create a POJO to model the year,
make and model of a car.
We'll call this class carPojo.
For that, we'll need some data members.
We want to make them immutable,
so we made them final.
We're going to add
a constructor and we
need some accessor methods to get at that data.
Notice that these accessor methods are all prefixed with get.
This is an older style
of doing accessor methods based on Java bins specifications.
More of a convention than anything.
You'll see in a minute that in
records they do this a little bit differently.
Let's see, what else do we need?
We need to override equals method.
If we're going to override equals method,
we should probably also implement the hashCode method.
These two go hand-in-hand.
Lastly, why not add a toString method.
You can see that for
this very simple set of data you're making model,
there's quite a bit of code that has to get
written for a plain old Java object.
Let's do the same thing.
But this time let's use
the record class and I've already done that,
so let's skip over here and boom, there it is.
That's all it takes using the record type to create
the same thing as we've just written all that code to do.
Notice here that we have declared a record.
We were calling it CarRecord,
so that is the name of our record.
Then we have in these parentheses what look to be some parameters.
Well, these are not really parameters,
these are what are called the components of the record.
When you look at these, you're really talking
about the components of the record,
you're making model or the components of our record.
It's important to note that the accessor methods are not get,
so I can't say
override public getYear,
because getYear is not a method in record.
The method is actually year.
Instead of getYear, you have year.
Instead of get make, you have make and so on.
Anyway as you can see,
you really get two things here.
First, you get a really concise way of declaring
class that's meant to do nothing more than carry data.
That's this declaration right here, very concise.
The second thing you get is code as documentation that
this car record is
a data carrier and it's because we've declared via record.
Somebody coming along and they
looking at your code and they can see
that car record is meant to be nothing
more than a plain old Java object.
There's a lot more to records than what I've just shown you,
but it would take a bit more time to go through that.
I'm going to do a follow on
video that will take a closer look at records.
In the meantime, if you want to look at
some documentation about records,
you can go to the Java 17 Java documentation,
look for the Java language updates,
and there you can find record classes.
This goes into good tutorial
and some detail about the record class in Java.
Another good resource here is in dove.java.
If you go to dove.java and use their little search feature,
you can search for records and you'll
probably come upon using the record model to model immutable data.
Another good tutorial goes into
a very good explanation of what records are and how they work,
and some of the details that I'll
cover in a screen recording to follow.
Thanks for watching and happy coding.
>> Thanks, David. That was great.
That's Part 1 of a two-part topic on Records.
Please check out the other part and also check out
the other topics in the Microsoft Java Beginner Series.
>> Thanks, Brian. Appreciate it.
[MUSIC]