シャドーイング練習: Records In Java - Full Tutorial - The Best New Java Feature You're Not Using - YouTubeで英語スピーキングを学ぶ

C2
records are a relatively new addition to Java
⏸ 一時停止中
207
文が短すぎたり長すぎる場合は、Editをタップして調整してください。
1
records are a relatively new addition to Java
2
that you can use to avoid a ton of boilerplate code in certain types of classes.
3
In this video we'll talk about exactly what records are,
4
how they can simplify your code,
5
and when and how you can implement them into your Java programs.
6
My name's John and I'm a lead Java software engineer.
7
I also have a full Java course available and a link down in the description so go check it out.
8
To understand why records are so useful it helps to know what programmers had to do before they existed,
9
which is probably what you are still doing if you're not using records yet.
10
Let's say you wanted to make a very simple Java class
11
that all it contained was a bunch of fields to carry data about something.
12
These kinds of classes are super common in the Java world.
13
Like for example if you had an employee class that carried the employee's name and their employee number.
14
Maybe that employee info exists in some database
15
and all this class has to do is hold that employee info and return it to the user.
16
So before records existed, here's everything you had to do.
17
Let's give it two private fields,
18
a string employee name, and an int employee number.
19
Let's also go ahead and make these final so they can't be modified after the employee object is created.
20
So because these two fields are final,
21
we want to be able to set them right when we create an employee object.
22
So we have to add them to a constructor.
23
So maybe our constructor is just public employee class and
24
that takes in the string name and the int employee number as parameters.
25
Then all it does is just set this.name equals name and this.employee number equals employee number.
26
Then let's go ahead and generate some getter methods for these two fields so they can be accessed from outside the class.
27
Next you also want a good two-string implementation
28
that returns all the data in the class in a nice
29
readable string format something like this next you're going to want
30
to override the hash code method with a good implementation as well now we're almost done
31
but not yet you're also going to want a good equals
32
method implementation to check whether one instance of this class is equal to another
33
so now finally after all of
34
that we can come back here to our main method
35
and create a new employee class object we'll just call it employee equals new employee class
36
and we pass in a name and employee number here in the constructor.
37
And there you go!
38
But that was a whole lot of code!
39
That's almost 50 lines of code for a class that does something so simple like hold two pieces of data.
40
But that's how you have to do it with a traditional Java class.
41
Now to compare, let's show how to do the exact same thing but using a record.
42
We just say public record.
43
We'll call it employee record.
44
It has a string name and an int employee number.
45
Open and close the curly brackets and that's it.
46
This single line of code gives us the equivalent of everything we just created in this employee class.
47
So now with just that we can go back here to our main method
48
and create an employee record object in the exact same way that we created the employee class object.
49
So we'll call this employee record equals new employee record
50
and pass in the name of Kramer and Kramer is employee number 54321.
51
So now let's break it down a bit.
52
To create a record first we say public record instead of public class.
53
That's because a record is a certain special type of class,
54
kind of like an enum is also a special type of class.
55
Next we just give it a name.
56
Here we're calling it employee record.
57
Next in parentheses we list out all the components
58
that we want to hold inside our record in a way that looks a lot like the parameters to a method.
59
So this is just all the different data elements that you want to hold in your record.
60
So here we have string name and int employee number.
61
So with just that, Java gives us a whole ton of other stuff for free.
62
First, it generates private fields for all the components that we listed here,
63
just like we did over here in our employee class.
64
These will also be generated as final fields by default,
65
just like we did in our class,
66
so that they can't be changed after an employee record object is created.
67
It also generates public getter methods for each of these components,
68
similar to the getters that we created here.
69
The only difference is that the getters that it automatically generates for a record don't have get in their method name.
70
They just have the name of that field.
71
So just to demonstrate that back over here in our main method,
72
if we wanted to print out this employee class object's name we would just call employee dot get name.
73
But to do the exact same thing with our employee record
74
object we would just have to call employee record dot name without the get
75
but if we go ahead and run our program we can see
76
that the functionality is the same next it will also generate
77
for you a certain type of constructor now you might know
78
that when you create a normal java class what it automatically creates for you behind the scenes even
79
if you don't make any constructor at all is a no args constructor like this that doesn't take any parameters.
80
When you create a record however it works a bit differently.
81
Instead what it creates is a constructor like this one
82
that takes in as parameters all of the different components that you listed up here in the record declaration.
83
Then it sets all of
84
that record's fields with the values in those parameters just like we did over here in our employee class constructor
85
that we made.
86
This type of constructor that is automatically generated when you create a record is known as a canonical constructor.
87
Even though we didn't explicitly create any constructor over here in our record,
88
we were still in our main class able to call a new constructor and pass in our two component values.
89
And that's because that constructor was automatically generated behind the scenes.
90
There's also some really cool things that you can do with constructors only in records
91
that you can't do in any other type of Java class that we'll talk about in just a little bit.
92
Finally, it'll also automatically generate implementations of the toString,
93
equals, and hash code methods.
94
Very similar to the ones that we created over here in our class.
95
So the toString method will give you the name of the record class
96
and then the name and value of all of its components.
97
And we can actually test out
98
that automatically generated toString method just by passing in the actual object to the system.out.println method
99
because when you pass in an object
100
that isn't a string to system out print line it automatically
101
calls the toString method to get the string representation of that object.
102
So if we go ahead and run our code we can see
103
that it prints out the contents of these objects in a very readable way.
104
So we can see that the record automatically created a toString
105
that has the name of the record and then the value of each of its components.
106
And it will generate equals and hash code methods
107
that will consider two objects of that record class equal if all of their components are equal to each other.
108
These three methods can be somewhat tedious to write,
109
and so often programmers will just skip implementing them.
110
And even if they do bother to do it,
111
they can be really easy to mess up,
112
especially the equals and hash code methods.
113
So with records, you don't have to worry about them at all.
114
It will automatically create them for you.
115
You are allowed to override those methods with implementations of your own,
116
but most of the time there's just no reason to do it.
117
One thing to note is that it does not generate any setter methods.
118
One of the main features of a record and one of the main reasons to use a record
119
is that they are immutable by default.
120
That just means that a record object cannot be changed once it's created.
121
Once your employee record object is created with a certain name and employee number,
122
you can no longer change this employee record's name or employee number to anything else.
123
So it really wouldn't make sense to have any setters created anyway.
124
Now there are a few other things that you'll have to know if you're going to be using records.
125
First, like any other class,
126
you can create instance methods if you want.
127
So for example, you might have some method public string name in uppercase.
128
And maybe all that does is just return name dot to uppercase.
129
And so you can call this method on an employee record object just like you would any other type of object.
130
So over here we can call employee record dot name in uppercase.
131
And if we run it,
132
we can see that we get the employee name in uppercase.
133
You can also create your own static methods as well.
134
So like public static void print whatever and that can just print whatever.
135
Of course to call a static method you should call it using the name of the class itself
136
and not a certain instance of that class.
137
So over here we would just call employee record with capital E capital R dot print whatever
138
and of course that will just print whatever.
139
You can also create static fields like you could in any other class.
140
For example, you could have a public static final string default employee name equals George.
141
However, you cannot define your own non-static instance fields.
142
So something like private string something,
143
you are not allowed to do this.
144
You'll get an error that says instance field is not allowed in record.
145
Any instance fields like this that you want have to be defined up here in your list of components.
146
Another thing is that records cannot extend any other class.
147
So up here if you try to do something like extends employee class you would get an error
148
that says no extends clause allowed for record.
149
All Java records implicitly extend the record class.
150
Kind of like all enums implicitly extend the enum class.
151
So because Java doesn't allow multiple inheritance your record can't be a child of any other class.
152
Records are also implicitly final classes,
153
which means that they can't be extended by any other class either.
154
You are allowed to put final in your record declaration,
155
but it's redundant to do so because it's already automatically going to be final.
156
However, you are allowed to implement interfaces in records.
157
So to do
158
that right up here after your record declaration like any other class you can just put in implements whatever interface you want.
159
So if you want you can implement the runnable interface.
160
It would probably be kind of weird for a record to be runnable
161
but if you want to do it you can do it.
162
You just have to implement this run method like you would for any other runnable.
163
I mentioned before that we'd talk a little bit more about constructors in records.
164
Remember that for records, Java will automatically create this canonical constructor
165
that takes in all of the components and just sets all of those fields based on those components that are passed in.
166
So the idea is once you use that canonical constructor to create an object of that record class,
167
all of its fields will be set and can never be changed.
168
The record only exists to carry that collection of data through your program.
169
However, you can also define your own constructors.
170
First, even though it gives you that canonical constructor for free,
171
you can override it with your own implementation if you want.
172
So if I want, I can create public employee record,
173
which takes in a string name and an int employee number.
174
So to override that default constructor,
175
it has to take in the exact same parameters in the exact same order.
176
One reason you might want to override the default constructor is
177
just to do some sort of validation on the values being set.
178
Like maybe it doesn't make sense for an employee number to be negative.
179
So you might do something like if the employee number is less than zero,
180
throw a new illegal argument exception.
181
Employee number cannot be negative.
182
After that though, you still have to set your records fields using the values passed in like this.
183
So this.name equals name and this.employeeNumber equals employeeNumber.
184
However, with records there's a really cool little shortcut you can use to override your canonical constructor
185
if you want and that's called a compact constructor.
186
This is something that is unique only to records.
187
All a compact constructor is is this exact same constructor except with the parameters removed and these assignments removed.
188
It will automatically add these same parameters in the same order,
189
and at the end of this constructor it will automatically set all of this record's fields with those parameters.
190
You can also have additional regular constructors that take in different sets of parameters if you want.
191
And for those all the normal constructor rules apply.
192
You can learn a ton more about Java constructors in this video here.
193
I think the ability to create this kind of compact constructor is really cool
194
and it really serves to illustrate exactly why records exist.
195
Records are just a super simple
196
and fast way to create a certain type of class
197
that we have to use all the time in the Java world and that's a class that just holds data.
198
Now remember we didn't even need any of this extra stuff we added
199
so just this is all
200
that we needed to create a full featured class whose purpose is to just hold and carry this data.
201
And
202
if you ask me that's a whole lot better than having
203
to write out all of this to do exactly the same thing.
204
If you enjoyed this video or learned something please let me know by leaving a like.
205
Be sure to subscribe for more java tutorials
206
and for my full java course just check out the link down in the description.
207
Thank you so much for watching I'll see you next time.

アプリをダウンロード

話したすべての文をAIが採点

スキャンしてダウンロード
スキャンしてダウンロード
TRENDING

人気動画

コンテキストと背景

この動画は、プログラミング言語Javaにおける新しい機能「レコード」についてのチュートリアルです。講師であるジョンは、Javaソフトウェアエンジニアとしての経験を活かし、レコードを使用する利点とその実装方法について詳しく説明しています。レコードの導入以前、プログラマーはクラスを作成する際に必要なボイラープレートコードが非常に多かったため、学習者はこれに合わせた英語の技術用語やフレーズを理解する必要があります。このセクションでは、プログラミングの観点から学ぶ英語を強化し、実践的な練習方法を提供します。

日常コミュニケーションのためのトップ5フレーズ

  • records are a relatively new addition to Java - 「レコードはJavaの比較的新しい追加機能です」
  • you can use to avoid a ton of boilerplate code - 「多くのボイラープレートコードを避けるために使用できます」
  • let’s show how to do the exact same thing - 「全く同じことを行う方法を示しましょう」
  • this single line of code gives us the equivalent of everything - 「この一行のコードは、すべての等価なものを提供します」
  • we can come back here to our main method - 「ここに戻ってメインメソッドに入ることができます」

ステップバイステップシャドーイングガイド

この動画の内容をシャドーイングすることは、プログラミングに関連する英語の技能向上に役立ちます。以下の手順に従って、効果的にシャドーイングを行いましょう:

  • 字幕を表示しながら動画を視聴する:まずは、動画を見ながら英語の字幕を確認しましょう。これにより、文の構造や語彙を理解しやすくなります。
  • 短いフレーズごとに止める:一度に長い部分を模倣するのではなく、短いフレーズごとに動画を一時停止し、その後に自分の声で繰り返します。
  • 発音に注目する:特にテクニカルな言葉やフレーズに対する発音に注意を払いましょう。たとえば、「boilerplate」や「record」の発音を確認し、自分のスピーキング練習に取り入れます。
  • 録音して自分の声を聞く:自分の声を録音して再生し、どの部分が改善できるかを考えます。これにより、英語スピーキングスキルを客観的に確認できます。
  • YouTubeで英語学習を継続する:この動画の内容を習得したら、他のYouTubeのプログラミングチャンネルやIELTSスピーキング対策に挑戦してみましょう。

このようにして、シャドーイングを通じて英語力を高め、技術的な会話を滑らかに行えるようになることを目指しましょう。動的な練習を行うことで、実際の会話の中でも効果的に応用できるスキルを身につけられます。

シャドーイングとは?英語上達に効果的な理由

シャドーイング(Shadowing)は、もともとプロの通訳者養成プログラムで開発された言語学習法で、多言語習得者として知られるDr. Alexander Arguelles によって広く普及されました。方法はシンプルですが非常に効果的:ネイティブスピーカーの英語を聞きながら、1〜2秒の遅延で声に出してすぐに繰り返す——まるで「影(shadow)」のように話者を追いかけます。文法ドリルや受動的なリスニングと異なり、シャドーイングは脳と口の筋肉が同時にリアルタイムで英語を処理・再現することを強制します。研究により、発音精度、抑揚、リズム、連音、リスニング力、そして会話の流暢さが大幅に向上することが確認されています。IELTSスピーキング対策や自然な英語コミュニケーションを目指す方に特におすすめです。

コーヒーをおごる