Hi there this is determined to little. 2
Now let's look at this recommendation from Effective Java. 3
It is like I'm going to read. 4
And it says Do not use a rock by halves. 5
And you could sign any code that feel few right now which should not be using what archives. 6
So let's go ahead and look at what a rock is. 7
We know what a genetic type is. 8
And we also know what a parameterize like this barometer or stripe is simply a genetic type followed 9
by that type argument. 10
Right. 11
And the rabbi is also a genetic trait but without any following type argument. 12
And here is one example. 13
List is a genetic type but here we are instantiating it without using that type argument. 14
So we have a rock here and here is another example where the metal barometer is it not. 15
I know the problem here with using it wrong type is we would have the same behavior as we had before 16
. 17
Genetics was introduced that is compile time types if any is lost on we may run into runtime exceptions 18
. 19
You could type safety problems in the interrupt really some of this chapter we looked at how using object 20
as a type can lead to runtime exceptions. 21
If you recall for example it was because object type allowed us in replacing dollar Dokoupil or did 22
object with our DOT DOT DOT object on later or some other part of the code. 23
It led to a class Kostic section. 24
And that's because the developer who inserted a jot or tittle or data object was expecting a dollar 25
Dokoupil or data object. 26
But what he got was a Java. 27
A secure and or data object. 28
And that's because some other developer has research to object to Jaguar not ask your daddy. 29
And that black boot cost an exception. 30
So he said that using genetic type gives you the benefit of avoiding such runtime exceptions. 31
However if the current code still uses have R-Type than the benefit of generics is lost on we know that 32
you should try to detect most of such errors at compile time excellence rather than runtime. 33
And in that aspect Jenrette explains a very important role here is that realistic example. 34
Let's say we have developed a competitive shopping engine which for a given item fetches the price information 35
from multiple partner sources. 36
This would allow the user to buy the item from the source that offers the lowest price. 37
Here let's assume we have four different classes which are depicted using blue circles. 38
Honestly the four classes are part of a data integration component. 39
OK so the data integration component is responsible for integrating data from multiple sources. 40
Let's say each of these classes are responsible for fetching the price information off a given book 41
from one particular source. 42
She had the four sources are Amazon Barnes and Noble have dot com and check on the user enters and there's 43
the book title in the browser. 44
We get the corresponding ISBN which is basically a unique number for the book on the Bassett along with 45
an MBA. 46
Idlest object to each of the individual objects that are responsible for fetching the price and the 47
method is appropriately named as price not out here to see my object is passed to the get price method 48
of each of the objects on the list object would be populated with the price information that is extracted 49
from the corresponding source. 50
No here to cut a declaration would be to get price signature on the left side where the Christ like 51
is used. 52
But let's assume the developer who wrote the method in the third class which interacts with half.com 53
has used a rock on the Bedacht we would run and we are an exception. 54
Let's assume that in the court this is how it looks like. 55
Let's say you have this method called fetch data which takes an ISBN of a particular item unfetter the 56
price information from all the different sources. 57
It just creates an our eldest starting the price information that is fetched from the different sources 58
. 59
The price information would be off by double as you can see to fetch the price information from Barnes 60
Noble. 61
It uses the class card be an integrator where B and stands for Bunson noble and in works its get price 62
method. 63
Similarly it also in books the get price method in half and degrader to fetch price from have not come 64
on let's assume that this is the code for get price in half integrator. 65
And as mentioned earlier raht type is used here for the second parameter in the first statement. 66
You may umed that we are in working of episodes of half dot com are not passing the ISBN. 67
Let's assume the method in most of Apso based on parts of the price information from the response under 68
don't set the price information is off indeed. 69
Under developer access to the prices are less without converting it to had doubled since the idealist 70
is apartheid it happily accepts that and no one's concordance returns from get price motet the price 71
written by half dot com is being extracted. 72
And we know that it would give us a cost cost exception because the compiler a place an implicit cast 73
on price which is of by and cannot be converted to a double. 74
So we lost compile time typesafe the benefit that a genetic type offers just because of using a rock 75
I get by method in half and integrator must have used a parametrized. 76
In fact even the compiler would give a warning if a rock type is used. 77
We will look at that in a short time so that we will do next. 78
So using it type is dangerous. 79
OK so the next question would be why did the language designers support her archives. 80
They should have made the compiler through an exception and the reason it was supported was for the 81
sake of interoperability that legacy code Gen X was introduced almost a decade after Java came into 82
existence. 83
And so there was tons of Java code that was already written Sarnoff methods were using collection pipes 84
as pipes for parameters and they all have to work with the new code. 85
So the hot water all types. 86
But in any new code that you write you must not use the archives as item 23 to commence although you 87
should not use archives. 88
There are only two instances where wrought by must be used. 89
And that has to do with type in Asia. 90
And here's a first instance wrought by have to be used in class. 91
We know that such a class literal would return the corresponding class object. 92
So the first example of it a raw type is used is valid. 93
The second example with a parameterized type is used is invalid. 94
So we get a compiler error here and here is the second instance we must use start type when using instance 95
of operator here in the first example we are checking if object reference or is an instance offset and 96
it needs to get Rotty in the second example. 97
It is a parameter Christ type. 98
And it would give us compiler error. 99
So these are not exceptions to the recommendation that are rock types. 100
It should not be used in new code. 101
Not that I don't want to eclipse to do a short demo. 102
OK here we are in the genetics demo class. 103
And a new method has been written called rock type dust or this demo. 104
So let's go ahead and look at what it has. 105
So the best. 106
It has the same example that we have seen in the slides. 107
So here we have an ISBN number. 108
It's on my S-B a number on B how the idlest object which will store the priceless information. 109
And I think that we how only half and degrader on we are invoking the get price method that ISBN as 110
well as this crisis list. 111
OK. 112
It's a list of double in the lecture we said that this wouldn't be popular with all the price information 113
of this particular ISBN and the place also to extract it from different sources. 114
And here we have only one source. 115
So let's go here. 116
And this is a half integrator it's simply a class in the same class generics Adem on and it's a static 117
Mod. undertakes ISBN. 118
And here is the prices. 119
Here is the wrong pipe. 120
And we should not be using it. 121
So here if a house or whatever the compiler says that this is wrong. 122
So it's giving us a warning. 123
This yellow line here it says reference estrogenic bitless E should be parameterised. 124
It's giving us a warning that it's not safe to do that. 125
Who can hear. 126
We are simply adding an A. 127
This is supposed to how only double but we are guarding an ant and if we go back we are once again after 128
they will Keeshan we are extracting the zero element which would be the price information. 129
And it is being assigned to the price which is of double because they're double up he's expecting it 130
to be doubled. 131
OK. 132
But this will give us a runtime exception class cost exception because we know that the compiler would 133
have inserted and Bissette costs to double. 134
So if we run this as you can see this is the output we are inside Iraq type test and this is from the 135
previous demo. 136
So it's this classic US exception Jaguar Dorte land are in danger which is inside the place is data 137
structure cannot be cast to outline or double. 138
So we should follow the item 23 It says Do not use rock pipes in your code so it should have been put 139
on Christ type. 140
OK. 141
And if we make it up but I mean Christ type here if we see of double. 142
In this case we get a compiler error because we are trying to add an 8. 143
So compiler will not allow us to do that. 144
So let me just delete it. 145
You can go back. 146
Now lets look at the second example here. 147
OK here we how clueless all strengths. 148
The first list has a b and c and the second has B C and D and we have this method called us get called 149
one element elements called which basically gives us the calling off the number of elements that are 150
in common between these two. 151
OK here we have B and C B and C Those are the common elements 2 elements. 152
OK and let's look at this matter. 153
You get common elements gone. 154
This is how it is. 155
Once again it has it all type. 156
And the reason I included this example is because generally a purse especially a noodle or perks would 157
be good to use rock type if the method is very generic. 158
In this case it's giving the number of common elements between the two us. 159
Now the elements can be strings or integers or doubles or whatever. 160
So it is a very generic method. 161
So in such a case developers might be tempted to use the archives we run this. 162
So in this case it's just that I'm in contest too because there are two elements in common here. 163
So for this there is always the risk of the role of inserting something in while there's less. 164
OK. 165
And in the calling code that hoper might run into runtime exception that may be rare but it is always 166
possible. 167
It depends on the car in the context on the matter on the upper. 168
So you never know. 169
So we should not be using our types as the as the item quoted we suggest. 170
So in such a case where we want to have the matter is very generic and we don't know what the limit 171
is or we don't care. 172
So that is when the tol'able might be dumb to use this but the recommendation for such a scenario where 173
we do not know what the limit is or we do not care what that amount is is to use something cart unbonded 174
wild card and that is something we will see in the next lesson. 175
All right then. 176
Thank you. 177
And this Dischord is available in the resource section. 178
Happy Coding