[MUSIC]
>> Greetings everyone, and welcome back to
Microsoft's Java for Beginner series.
My name is Brian Benz.
I'm a Cloud advocate at Microsoft.
I'm joined by David Grieve.
He's going to talk to us about Records.
David, do you want to tell us a little bit about yourself,
and what's in this recording?
>> Sure. My name is David Grieve.
I work for the Java Engineering Group here at Microsoft.
This is a follow on recording to records,
and this particular recording
will dive into Records a little more deeply.
We'll actually use JShell to explore Records.
Probably reinforce what you've learned in the first video.
What I'm going to do,
is I'm going to jump into JShell.
If you haven't used JShell before, it's awesome.
It lets you play around with Java.
You can type in
pretty much any Java thing and it is
immediately evaluated and the results appear there on the screen.
What I'll do is I'll just create here
in JShell the record we just looked at.
I'll create a record,
CarRecord, and I'll give it components.
Again, these are components.
Here at the end,
we'll just type. I'm sorry.
Type, year String for the make,
String for the model,
and of course my braces is there.
I've created this CarRecord.
Let's create one of these things,
actually instantiate one of these things.
I'll call this myCar and I'll do a new CarRecord.
There's nothing different here from using this,
just like a normal class, right.
New CarRecord this how you'd create any old class.
Oh, yes the 1966.
Let's pick the Jaguar,
and let's pick the XKE race car.
Unfortunately, not a car I own,
but, boy, I like to.
Now we have a record called myCar.
Let's take a look at what it's got.
If I want to get to the model of this car,
I can do myCar.model and it returns the value.
We do the same thing with the year and the make,
and do year and so on.
You notice that the difference here
between what I had in the plain old Java object,
which was a get year or a get model.
This is just the name of the component.
This is a more modern approach to Java.
The whole get method thing
came out a long time ago part of Java beings.
I think this is much easier
to more modern way of approaching the access or methods.
Let's see. Let's look at equals here.
You just create another Car.
I create my other car. That's good enough.
I'll just do a sorry and I'll just do a
myCar.equals
other Car, my other Car.
That's true. I mean,
which we do expect because they're both 1966 Jaguars,
XKEs and to look into year, make and model.
Of course, if I had two of these in my garage,
one would be a hardtop,
one would be a convertible,
but they'd both be British racing green of course.
There's some other things to know about Records.
Records are final.
You cannot inherit from a Record.
You can't extend the Record because they are final.
The Record itself cannot extend another class.
You can't say CarRecord extends vehicle, it can't happen.
The Record type already
extends from a class
called record so you can't extend any further.
You can however implement interfaces.
I could implement serializable here.
I could say Record implements serializable if I wanted to.
Actually, Records are very good thing to use with serializable,
because the compiler knows that it's a record.
It's a lot more efficient than
serializing records in normal classes.
Let's see, what else?
Records are what they call shallowly immutable so if I pass,
let's just do this and say,
I've got the record.
Example, List,
String, List.
I've created this example.
Now, if I created an instance of this and I gave it a list,
if I were to modify that list outside of this record,
the list is going to be modified inside the record too.
That's what it means by shallowly immutable,
not changing the variable that's
holding the list inside the record.
But you can still modify that outside.
If you are worried about immutable,
things like lists, hash tables, things like that,
that can be modified outside,
you either want to make a copy of them or,
make them unmodifiable from the get go.
Like I said, the record has a canonical constructor,
and it also has this thing called a compact constructor.
Let me just give you an example of a compact constructor.
Go back to myCar record.
Carefully here. The compact constructor
looks like a normal constructor.
I'm going to make a Car record,
but this is where it's different.
There are no parameters given to the compact constructor.
What can you do with this?
I mean, you can't pass any data to it,
so what can you do with this?
Well, you can think of this is
the a bit of
code that gets called no matter what constructor you call.
If I call my canonical constructor,
it's going to use this compact constructor if it's available,
and so this is a perfect place to do
things like variable validation.
You want to validate the data that's being passed to this.
Yeah. You can say, objects.requireNonNull(make).
Now if I create myCar, that was this.
This was myCar.
I'm just cheating here.
If I pass null here,
boom it's going to.
We've myCar, I guess not.
There we go, exception says
requires non null, that's coming from here.
Notice that it's coming from CarRecord init.
Anytime you see this init method, that's a constructor.
That's the compact constructor.
Great place to glom on to
do validation of the parameters that are being passed.
>> Thanks, David that was great.
That's part 2 of Records.
Check out the first part and also check out
the other topics in Microsoft's Java for Beginners series.
>> Thanks, Brian.