7- Type Erasure

Jun 20, 2026 06:03 · 4:04 · English · Whisper Turbo · 2 speakers
இந்த நகல் காலாவதியாகும் 24 நாட்கள். நிலையான சேமிப்புக்காக மேம்படுத்தல் →
காட்டு
0:03
S… Speaker 2 (7- Type Erasure)
So now that you have a good understanding of what generics are,
0:06
S… Speaker 2 (7- Type Erasure)
let's see how they work under the hood.
0:08
S… Speaker 1 (7- Type Erasure)
So I've removed the constraint on this type parameter,
0:11
S… Speaker 2 (7- Type Erasure)
we only have T.
0:12
S… Speaker 1 (7- Type Erasure)
Now let's recompile our code.
0:15
S… Speaker 2 (7- Type Erasure)
So on the top,
0:16
S… Speaker 2 (7- Type Erasure)
from the build menu,
0:17
S… Speaker 2 (7- Type Erasure)
let's go to build project.
0:19
S… Speaker 2 (7- Type Erasure)
Next we open the project window,
0:22
S… Speaker 2 (7- Type Erasure)
select the generic list class,
0:25
S… Speaker 2 (7- Type Erasure)
and from the view menu we go to show bytecode.
0:28
S… Speaker 2 (7- Type Erasure)
Here we can see the actual bytecode that is generated as
0:33
S… Speaker 2 (7- Type Erasure)
a result of compiling our Java code.
0:35
S… Speaker 2 (7- Type Erasure)
Remember bytecode?
0:36
S… Speaker 2 (7- Type Erasure)
We talked about it in the first part of this series.
0:38
S… Speaker 2 (7- Type Erasure)
Bytecode is a platform independent representation of our Java code.
0:42
S… Speaker 1 (7- Type Erasure)
So when we compile our Java code,
0:44
S… Speaker 2 (7- Type Erasure)
the Java compiler will produce this bytecode that can run on different platforms
0:49
S… Speaker 2 (7- Type Erasure)
like Windows,
0:49
S… Speaker 2 (7- Type Erasure)
Mac, and Linux.
0:50
S… Speaker 1 (7- Type Erasure)
Now,
0:51
S… Speaker 2 (7- Type Erasure)
when we run our program,
0:52
S… Speaker 2 (7- Type Erasure)
the Java virtual machine will get this bytecode and convert it to the native
0:57
S… Speaker 2 (7- Type Erasure)
code for the target platform.
0:59
S… Speaker 2 (7- Type Erasure)
So if you run this code on Windows,
1:00
S… Speaker 2 (7- Type Erasure)
we have an implementation of Java virtual machine that knows how to
1:04
S… Speaker 2 (7- Type Erasure)
convert this bytecode into native code for Windows.
1:07
S… Speaker 2 (7- Type Erasure)
We also have an implementation of Java virtual machine for Mac,
1:10
S… Speaker 2 (7- Type Erasure)
for Linux and other operating systems.
1:13
S… Speaker 1 (7- Type Erasure)
Now,
1:14
S… Speaker 1 (7- Type Erasure)
why do we care about this?
1:15
S… Speaker 2 (7- Type Erasure)
Well, if you scroll up,
1:17
S… Speaker 2 (7- Type Erasure)
you can see different pieces of the generic list class.
1:21
S… Speaker 1 (7- Type Erasure)
For example,
1:22
S… Speaker 2 (7- Type Erasure)
over here we have our two private fields.
1:24
S… Speaker 2 (7- Type Erasure)
Here we have the items field and the count field.
1:27
S… Speaker 2 (7- Type Erasure)
Now look at the type of the items field.
1:29
S… Speaker 2 (7- Type Erasure)
It's an object.
1:30
S… Speaker 1 (7- Type Erasure)
Well, more accurately,
1:31
S… Speaker 2 (7- Type Erasure)
it's an object array.
1:32
S… Speaker 1 (7- Type Erasure)
Also,
1:33
S… Speaker 1 (7- Type Erasure)
if you scroll down,
1:34
S… Speaker 2 (7- Type Erasure)
you can see our add method.
1:36
S… Speaker 2 (7- Type Erasure)
Now look at the type of the parameter of this method.
1:39
S… Speaker 1 (7- Type Erasure)
It's the object class.
1:40
S… Speaker 1 (7- Type Erasure)
So when we compile this code,
1:43
S… Speaker 2 (7- Type Erasure)
the Java compiler will replace all these T's with the object class.
1:47
S… Speaker 2 (7- Type Erasure)
And that means,
1:48
S… Speaker 2 (7- Type Erasure)
internally,
1:49
S… Speaker 2 (7- Type Erasure)
this class is implemented like the non -generic list that we created earlier.
1:53
S… Speaker 1 (7- Type Erasure)
This class over here.
1:57
S… Speaker 2 (7- Type Erasure)
So when we compile our code,
1:58
S… Speaker 2 (7- Type Erasure)
our generic list will end up looking like this in bytecode.
2:01
S… Speaker 2 (7- Type Erasure)
the difference between these two implementations is compile time type
2:05
S… Speaker 1 (7- Type Erasure)
safety.
2:06
S… Speaker 2 (7- Type Erasure)
So when we use the generic implementation,
2:08
S… Speaker 2 (7- Type Erasure)
the java compiler will check for type errors at compile time.
2:12
S… Speaker 2 (7- Type Erasure)
But internally,
2:13
S… Speaker 2 (7- Type Erasure)
our integers,
2:14
S… Speaker 2 (7- Type Erasure)
our strings,
2:15
S… Speaker 2 (7- Type Erasure)
our user objects are stored in an array of objects.
2:18
S… Speaker 2 (7- Type Erasure)
And this is not because we have set this field to a new object array.
2:22
S… Speaker 1 (7- Type Erasure)
Even if we don't do this,
2:23
S… Speaker 2 (7- Type Erasure)
the java compiler will still replace all these t's with the object class.
2:28
S… Speaker 2 (7- Type Erasure)
Now let's see what happens when we apply a constraint.
2:30
S… Speaker 2 (7- Type Erasure)
So here I'm going to type extends number.
2:33
S… Speaker 2 (7- Type Erasure)
Now we recompile our code then
2:38
S… Speaker 2 (7- Type Erasure)
from the project window we select generic list and go
2:43
S… Speaker 2 (7- Type Erasure)
to show bytecode.
2:44
S… Speaker 1 (7- Type Erasure)
Take a look.
2:46
S… Speaker 1 (7- Type Erasure)
Now the type of this field is number.
2:49
S… Speaker 1 (7- Type Erasure)
Similarly,
2:50
S… Speaker 2 (7- Type Erasure)
if you look at the add method,
2:52
S… Speaker 2 (7- Type Erasure)
the type of the parameter is changed to number.
2:55
S… Speaker 2 (7- Type Erasure)
So when we apply a constraint,
2:56
S… Speaker 2 (7- Type Erasure)
the Java compiler will replace all these T's based on
3:00
S… Speaker 2 (7- Type Erasure)
the constraint that we have set here.
3:02
S… Speaker 1 (7- Type Erasure)
Here our constraint is the number class,
3:04
S… Speaker 2 (7- Type Erasure)
so all these T's will be replaced with the number class.
3:07
S… Speaker 1 (7- Type Erasure)
If you have an interface,
3:08
S… Speaker 2 (7- Type Erasure)
let's say,
3:09
S… Speaker 2 (7- Type Erasure)
comparable,
3:10
S… Speaker 2 (7- Type Erasure)
all these T's will be replaced with the comparable interface.
3:14
S… Speaker 2 (7- Type Erasure)
Now what if we have two constraints,
3:17
S… Speaker 1 (7- Type Erasure)
like comparable and clonable.
3:19
S… Speaker 2 (7- Type Erasure)
Let's take a look.
3:20
S… Speaker 1 (7- Type Erasure)
So,
3:21
S… Speaker 2 (7- Type Erasure)
we recompile the code,
3:22
S… Speaker 1 (7- Type Erasure)
then
3:25
S… Speaker 2 (7- Type Erasure)
look at the bytecode,
3:26
S… Speaker 1 (7- Type Erasure)
take a look.
3:27
S… Speaker 2 (7- Type Erasure)
The type of the items field is comparable,
3:30
S… Speaker 2 (7- Type Erasure)
and similarly,
3:31
S… Speaker 2 (7- Type Erasure)
the type of the parameter of the add method is comparable.
3:37
S… Speaker 2 (7- Type Erasure)
So when we have multiple interface constraints,
3:39
S… Speaker 2 (7- Type Erasure)
the Java compiler will take the leftmost one and use that to
3:43
S… Speaker 2 (7- Type Erasure)
replace all these t's.
3:45
S… Speaker 2 (7- Type Erasure)
This is called type erasure,
3:46
S… Speaker 2 (7- Type Erasure)
which means the Java compiler erases these type parameters and
3:51
S… Speaker 2 (7- Type Erasure)
replaces them with a class or an interface depending on the constraints.
3:54
S… Speaker 2 (7- Type Erasure)
If there are no constraints,
3:56
S… Speaker 2 (7- Type Erasure)
all these t's are replaced with the object class.
3:58
S… Speaker 1 (7- Type Erasure)
So this is how generics work under the hood.

This transcript was generated by AI (automatic speech recognition). May contain errors — verify against the original audio for critical use. AI policy

❤️ STT.ai ஐ நேசிக்கிறீர்களா? உங்கள் நண்பர்களுக்குச் சொல்லுங்கள்!
சுருக்கம்
இந்த நகலெடுப்புக்கான AI சுருக்கத்தை உருவாக்க சுருக்கம் என்பதை க்ளிக் செய்யவும்.
சுருக்கப்படுகிறது...
இந்த நகலை AI க்குக் கேட்கவும்
இந்த நூலைப் பற்றி ஏதாவது கேட்கவும் - AI தொடர்புடைய பகுதிகளைக் கண்டுபிடித்து பதிலளிக்கும்.