માત્ર બતાવી રહ્યા છે
0:03
S… Speaker 1 (3- A Poor Solution)
One way to solve the problem in the last video is to use an area of objects here
0:07
S… Speaker 1 (3- A Poor Solution)
because the object class is the base or the parent of all reference types
0:11
S… Speaker 1 (3- A Poor Solution)
in Java.
0:12
S… Speaker 1 (3- A Poor Solution)
Remember reference types?
0:13
S… Speaker 1 (3- A Poor Solution)
We talked about them in the first part of this series.
0:16
S… Speaker 1 (3- A Poor Solution)
To refresh your memory,
0:17
S… Speaker 1 (3- A Poor Solution)
we have two categories of types in Java.
0:20
S… Speaker 1 (3- A Poor Solution)
Primitive types and reference types.
0:22
S… Speaker 1 (3- A Poor Solution)
Primitive types are numbers,
0:24
S… Speaker 1 (3- A Poor Solution)
characters,
0:24
S… Speaker 1 (3- A Poor Solution)
and booleans.
0:25
S… Speaker 1 (3- A Poor Solution)
Everything else is a reference type.
0:27
S… Speaker 1 (3- A Poor Solution)
So if we use an array of objects here,
0:29
S… Speaker 1 (3- A Poor Solution)
we can store anything in this array.
0:31
S… Speaker 1 (3- A Poor Solution)
For example,
0:32
S… Speaker 1 (3- A Poor Solution)
we can store user objects because the user class extends the object
0:36
S… Speaker 1 (3- A Poor Solution)
class.
0:36
S… Speaker 1 (3- A Poor Solution)
So let's replace all these integers with the object class.
0:39
S… Speaker 1 (3- A Poor Solution)
So here we have an object array and here
0:44
S… Speaker 1 (3- A Poor Solution)
as well, the parameter of the add method should be an object and
0:48
S… Speaker 1 (3- A Poor Solution)
also the return type of the get method should be an object.
0:53
S… Speaker 2 (3- A Poor Solution)
Now,
0:54
S… Speaker 1 (3- A Poor Solution)
back to our main class.
0:56
S… Speaker 1 (3- A Poor Solution)
We can store numbers in this array.
0:58
S… Speaker 1 (3- A Poor Solution)
We can also store strings.
1:01
S… Speaker 1 (3- A Poor Solution)
We can also store user
1:05
S… Speaker 1 (3- A Poor Solution)
objects.
1:05
S… Speaker 1 (3- A Poor Solution)
So that's great,
1:08
S… Speaker 1 (3- A Poor Solution)
isn't it?
1:08
S… Speaker 1 (3- A Poor Solution)
Not really.
1:09
S… Speaker 1 (3- A Poor Solution)
This is a very bad solution for a couple of reasons.
1:12
S… Speaker 1 (3- A Poor Solution)
But before we talk about these reasons,
1:13
S… Speaker 1 (3- A Poor Solution)
I've got a question for you.
1:15
S… Speaker 1 (3- A Poor Solution)
Earlier I told you that the object class is the parent of all reference
1:19
S… Speaker 1 (3- A Poor Solution)
types, so we can store any objects in this list.
1:21
S… Speaker 1 (3- A Poor Solution)
However,
1:22
S… Speaker 1 (3- A Poor Solution)
this integer here,
1:23
S… Speaker 1 (3- A Poor Solution)
this is a primitive value,
1:25
S… Speaker 1 (3- A Poor Solution)
it's not an object,
1:26
S… Speaker 1 (3- A Poor Solution)
it's not a reference type.
1:27
S… Speaker 1 (3- A Poor Solution)
So how can we store it here?
1:29
S… Speaker 2 (3- A Poor Solution)
Well,
1:30
S… Speaker 1 (3- A Poor Solution)
when we compile this code,
1:31
S… Speaker 1 (3- A Poor Solution)
the Java compiler will convert this code to something like this.
1:35
S… Speaker 1 (3- A Poor Solution)
Integer dot value of 1.
1:38
S… Speaker 1 (3- A Poor Solution)
So we have this integer class in Java,
1:40
S… Speaker 1 (3- A Poor Solution)
it's a reference type,
1:41
S… Speaker 1 (3- A Poor Solution)
it derives from the object class.
1:43
S… Speaker 1 (3- A Poor Solution)
And this class has a static method,
1:45
S… Speaker 1 (3- A Poor Solution)
valueof,
1:46
S… Speaker 1 (3- A Poor Solution)
which returns a new instance of the integer class.
1:50
S… Speaker 1 (3- A Poor Solution)
So this is the reason why we can pass a primitive value to this method.
1:54
S… Speaker 1 (3- A Poor Solution)
By the same token,
1:55
S… Speaker 1 (3- A Poor Solution)
we have a wrapper class for every primitive type in Java.
1:59
S… Speaker 1 (3- A Poor Solution)
We have float,
2:00
S… Speaker 1 (3- A Poor Solution)
we have double,
2:01
S… Speaker 1 (3- A Poor Solution)
boolean,
2:02
S… Speaker 1 (3- A Poor Solution)
character,
2:03
S… Speaker 1 (3- A Poor Solution)
and so on.
2:04
S… Speaker 2 (3- A Poor Solution)
Okay,
2:05
S… Speaker 1 (3- A Poor Solution)
so let's revert this back.
2:08
S… Speaker 1 (3- A Poor Solution)
What are the problems with this implementation?
2:10
S… Speaker 2 (3- A Poor Solution)
Well,
2:11
S… Speaker 1 (3- A Poor Solution)
the first problem is that if you want to get the first item in this list,
2:13
S… Speaker 1 (3- A Poor Solution)
we call the get method.
2:16
S… Speaker 2 (3- A Poor Solution)
Now,
2:18
S… Speaker 1 (3- A Poor Solution)
we know that this is an integer,
2:19
S… Speaker 1 (3- A Poor Solution)
so we may hope to store it in an integer variable called number.
2:23
S… Speaker 1 (3- A Poor Solution)
but we have a compilation error because this get method returns an object,
2:28
S… Speaker 1 (3- A Poor Solution)
but we expect an integer.
2:29
S… Speaker 1 (3- A Poor Solution)
So here we have to explicitly cast the result to
2:34
S… Speaker 1 (3- A Poor Solution)
an integer.
2:34
S… Speaker 1 (3- A Poor Solution)
And this makes our code a little bit verbose and noisy.
2:37
S… Speaker 1 (3- A Poor Solution)
The second problem is that if we use the wrong type here,
2:40
S… Speaker 1 (3- A Poor Solution)
we'll get an invalid cast exception.
2:42
S… Speaker 1 (3- A Poor Solution)
For example,
2:43
S… Speaker 1 (3- A Poor Solution)
let's change 0 to 1.
2:45
S… Speaker 1 (3- A Poor Solution)
We know that the second item in this list is a string.
2:48
S… Speaker 1 (3- A Poor Solution)
What if we accidentally try to cast it to an integer?
2:51
S… Speaker 1 (3- A Poor Solution)
We run our program,
2:55
S… Speaker 1 (3- A Poor Solution)
we get a class cast exception.
2:59
S… Speaker 1 (3- A Poor Solution)
Now the problem here is that we will not be aware of bugs like this
3:03
S… Speaker 1 (3- A Poor Solution)
until we run our application and test all the functions.
3:06
S… Speaker 1 (3- A Poor Solution)
So we can only identify these problems at runtime.
3:09
S… Speaker 1 (3- A Poor Solution)
It would be great if we could catch these problems at compile time.
3:13
S… Speaker 1 (3- A Poor Solution)
And that's what generics are for.
3:15
S… Speaker 1 (3- A Poor Solution)
In the next video,
3:17
S… Speaker 1 (3- A Poor Solution)
I will show you how generic classes help us solve these problems.

આ ટ્રાન્સક્રિપ્ટ AI (આપોઆપ બોલવાની ઓળખ) દ્દારા ઉત્પન્ન થયેલ છે. ભૂલો સમાવી શકે છે - મહત્વપૂર્ણ વપરાશ માટે મૂળભૂત ઓડિયો સાથે ચકાસો. AI નીતિ

❤️ STT.ai ને પ્રેમ કરો છો? તમારા મિત્રોને કહો!
સાર
આ ટ્રાન્સક્રિપ્ટના AI સારને ઉત્પન્ન કરવા માટે સારાંશ પર ક્લિક કરો.
સારાંશ કરી રહ્યા છીએ...
આ ટ્રાન્સક્રિપ્ટ વિશે AI ને પૂછો
આ ટ્રાન્સક્રિપ્ટ વિશે કંઈપણ પૂછો - AI સંબંધિત વિભાગો શોધશે અને જવાબ આપશે.