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