1

00:00:01,260  -->  00:00:05,400
Know the art of boxing makes a lot more or less verbose.

2

00:00:05,430  -->  00:00:11,700
However one of the effective dollar items cautions about some issues that we may run into if baulks

3

00:00:11,700  -->  00:00:14,090
primitives are used carelessly.

4

00:00:14,610  -->  00:00:21,230
So let's look at this item and we will also test all the examples that we are going to discuss.

5

00:00:21,290  -->  00:00:22,880
Here's the recommendation it is.

6

00:00:22,880  -->  00:00:28,590
Item 49 and it says prefer primitive types to box primitives.

7

00:00:28,590  -->  00:00:32,460
Let's look at the reasons behind this recommendation.

8

00:00:32,460  -->  00:00:37,870
One reason to prefer primitives is because they are both time and space efficient.

9

00:00:38,430  -->  00:00:40,750
So they are faster and also simpler.

10

00:00:41,460  -->  00:00:47,520
If baulks primitives are used carelessly and if auto boxing is also involved then we may face serious

11

00:00:47,520  -->  00:00:48,880
performance issues.

12

00:00:49,230  -->  00:00:53,610
Let's look at this with this example that is taken from the effect of July.

13

00:00:54,090  -->  00:00:58,610
It simply calculates the sum of all positive and values.

14

00:00:58,860  -->  00:01:05,760
That is we are adding numbers from 0 to integer not Max underskirt value which we know represents the

15

00:01:05,850  -->  00:01:07,210
max value off.

16

00:01:07,260  -->  00:01:08,100
Indeed.

17

00:01:09,150  -->  00:01:15,270
The result of this would be a very large value and hence a longer metric is needed as it is not big

18

00:01:15,270  -->  00:01:17,620
enough to hold the result.

19

00:01:17,730  -->  00:01:21,320
The program works correctly but is much slower than it should be.

20

00:01:21,720  -->  00:01:28,650
And the reason for this is due to the usage of long wrapper class instead of primitive long under-sea

21

00:01:28,650  -->  00:01:35,850
while using long wrapper class would cause the slowness here plus operator has two operands a primitive

22

00:01:35,850  -->  00:01:37,480
long represented by variable.

23

00:01:37,480  -->  00:01:40,730
I under the box primitive sum.

24

00:01:40,920  -->  00:01:46,380
If you recall then learning about arithmetic operators we discussed funeral's like operant promotion

25

00:01:46,380  -->  00:01:52,780
rule unmixed operant April their operatives were promoted from one type to another.

26

00:01:52,800  -->  00:01:57,890
All these rules were discussed in the context of only primitive data types.

27

00:01:57,900  -->  00:02:04,090
However here one operand is an object reference which is a box primitive while the other is a primitive

28

00:02:04,100  -->  00:02:05,710
.

29

00:02:06,300  -->  00:02:10,830
In this case first a box primitive is unboxed and then the values added to.

30

00:02:10,830  -->  00:02:14,200
I finally get to the assignment operator.

31

00:02:14,310  -->  00:02:18,900
The resulting value is auto boxed and assigned to the variable sum.

32

00:02:19,440  -->  00:02:26,100
And this is happening how many times roughly due to the part of 31 times due to max underscore value

33

00:02:26,880  -->  00:02:32,700
that is roughly due to the part of Thirty-One long instances are being created and that is very very

34

00:02:32,700  -->  00:02:34,440
expensive.

35

00:02:34,440  -->  00:02:39,280
Let's actually run this program and look at the performance.

36

00:02:39,640  -->  00:02:42,040
Here is a method called very expensive.

37

00:02:42,310  -->  00:02:48,870
So let's just go ahead and measure its performance that is measure how long it takes for this court

38

00:02:48,870  -->  00:02:49,970
to execute.

39

00:02:50,070  -->  00:02:52,280
And here is the method in location.

40

00:02:52,440  -->  00:02:58,920
And here we are tracking the start time and for that we are in working method called Nano time from

41

00:02:58,920  -->  00:03:05,850
this classical system which is from that Jallow that Lanc package are nigricans the time in nanoseconds

42

00:03:05,850  -->  00:03:06,750
.

43

00:03:06,750  -->  00:03:07,880
Let's not worry about it.

44

00:03:07,890  -->  00:03:13,060
If you are interested you can just go ahead and look at this matter in the API.

45

00:03:13,290  -->  00:03:19,110
And we need to measure the elapsed time so we are in working the Nano time once again here.

46

00:03:19,110  -->  00:03:23,380
So this is the end time and so we are in order to get the elapsed time.

47

00:03:23,400  -->  00:03:29,850
We are subtracting the start time from the time and since that time is in nanoseconds we just divided

48

00:03:29,850  -->  00:03:34,500
by a million in order to convert the time into milliseconds.

49

00:03:34,500  -->  00:03:37,160
So let's just go ahead and run this program.

50

00:03:37,160  -->  00:03:38,950
It's already compiled

51

00:03:47,310  -->  00:03:50,580
so it's taking roughly around 7 seconds.

52

00:03:50,580  -->  00:04:06,630
Now let's go back and convert this and do a perimeter and let's compile and execute again.

53

00:04:06,630  -->  00:04:09,370
So it's just taking one point two seconds.

54

00:04:09,750  -->  00:04:15,810
So when we use the proper class it's about 5 times slower or roughly five times slower.

55

00:04:15,870  -->  00:04:22,920
So if you have a choice you should always use Amatil because it is much faster than the wrapper class

56

00:04:23,520  -->  00:04:26,120
especially if you are performing some operations like this.

57

00:04:26,340  -->  00:04:31,500
You should definitely go for the primitive type.

58

00:04:31,500  -->  00:04:36,930
There are a couple of more things that the item discusses And let's review them.

59

00:04:37,030  -->  00:04:44,400
We know the primitives are simple things on how only their values but we know that baulks primitives

60

00:04:44,640  -->  00:04:49,450
are just regular classes and so the how identities do that is.

61

00:04:49,470  -->  00:04:55,440
Each box primitive instance is a separate object which has a memory address and that memory address

62

00:04:55,530  -->  00:04:57,840
is basically the identity.

63

00:04:58,100  -->  00:05:05,830
So toolbox primitives can have same value but different identities so when you compare to box primitives

64

00:05:05,920  -->  00:05:12,360
with an equal to operator and identity comparison is done like in the case of any two object or Francis

65

00:05:12,360  -->  00:05:13,370
.

66

00:05:13,390  -->  00:05:19,180
So even if the box primitives have the same value applying equal to operator on them would return a

67

00:05:19,180  -->  00:05:22,310
false at different identities.

68

00:05:22,310  -->  00:05:28,450
Normally if a programmer is applying an equal to operator on toolbox primitives he or she is most likely

69

00:05:28,450  -->  00:05:32,890
expecting the proper values to be compared which is not going to happen.

70

00:05:33,340  -->  00:05:36,780
Let's just review this in code.

71

00:05:37,070  -->  00:05:43,440
Here is the new method called compare baulks primitives and it is added in the basics class.

72

00:05:43,450  -->  00:05:45,550
Here we are declaring two blocks primitives.

73

00:05:45,550  -->  00:05:50,980
Number one or number two I end up both of them are in each class with the same primitives zero.

74

00:05:51,040  -->  00:05:58,420
Here and here we are comparing them using an equality operator and we know that this should give us

75

00:05:58,420  -->  00:06:03,740
false because they both have different identities before compiling and executing this.

76

00:06:03,760  -->  00:06:07,060
Let's actually look at the solutions to the first solution.

77

00:06:07,060  -->  00:06:12,710
As you were in the interval you mattered are number one and you would also do the same thing on number

78

00:06:12,780  -->  00:06:18,670
two so that when you get you on extracting the primitives from both the box primitives and you are simply

79

00:06:18,670  -->  00:06:25,060
making a comparison of them and the second solution is that you would invoke equal matter or number

80

00:06:25,060  -->  00:06:32,030
one and you would just do this number and not equal of number to an equals method would compare the

81

00:06:32,040  -->  00:06:33,800
proper primitive values.

82

00:06:34,090  -->  00:06:41,590
So you can do it either using equals method or by unwrapping explicitly and doing a comparison.

83

00:06:41,650  -->  00:06:43,630
So these are the two solutions.

84

00:06:43,630  -->  00:06:46,720
And here is another interesting example.

85

00:06:46,720  -->  00:06:52,300
Here we have the box limit of number three which is initialized to 1 and we are just checking if Number

86

00:06:52,300  -->  00:06:54,170
1 is less than number 3.

87

00:06:54,190  -->  00:07:02,620
But unlike the equality operator here the less than operator it actually in-boxes bird the wrapper classes

88

00:07:03,190  -->  00:07:05,180
so in which case number one is zero.

89

00:07:05,200  -->  00:07:06,900
Or number three is one.

90

00:07:06,970  -->  00:07:10,030
So it's not doing an identity comparison or anything like that.

91

00:07:10,060  -->  00:07:16,380
It's simply going to compare the raw values that are wrapped inside these wrapped classes.

92

00:07:16,510  -->  00:07:19,040
So that's an important difference here.

93

00:07:19,240  -->  00:07:22,020
So let's just go ahead and execute this.

94

00:07:22,070  -->  00:07:23,570
It's already compiled

95

00:07:26,290  -->  00:07:28,360
as you can see no one equals to number.

96

00:07:28,370  -->  00:07:31,950
Gives false then the first solution is giving us true.

97

00:07:31,990  -->  00:07:35,440
An equals is also giving us true when we do this number one.

98

00:07:35,440  -->  00:07:36,610
Lesson Number 3.

99

00:07:36,640  -->  00:07:45,050
It also gives group because an on threes 3 1 1 1 0 and that's it.

100

00:07:47,020  -->  00:07:48,870
In the final example we just saw.

101

00:07:48,940  -->  00:07:51,850
We use that as an operator on this order.

102

00:07:51,910  -->  00:07:53,930
Auto unboxing was performed.

103

00:07:54,640  -->  00:08:00,250
If you recall a comparison operator does listen we mentioned up less than less than or equal to greater

104

00:08:00,250  -->  00:08:06,580
than and greater than not equal to operators would look at only numeric primitives a non-Mitt object

105

00:08:06,580  -->  00:08:07,690
references.

106

00:08:08,200  -->  00:08:13,970
But then we just saw that box primitives which are object references can be used with them.

107

00:08:14,140  -->  00:08:21,460
So that's an exception among object references only box primitives are permitted with these four operators

108

00:08:21,480  -->  00:08:21,650
.

109

00:08:21,790  -->  00:08:26,010
That is less than less than or equal to greater than and greater than or equal to.

110

00:08:26,400  -->  00:08:29,590
And the box Plimmer those are be unwrapped.

111

00:08:29,590  -->  00:08:37,080
If you use other object references with this for operators you will end up with compilers.

112

00:08:37,630  -->  00:08:44,100
So we saw that an identity comparison is done then both operands of equal to OPERATOR Are baulks primitives

113

00:08:44,110  -->  00:08:44,850
.

114

00:08:45,050  -->  00:08:47,750
Holler when it comes to Mixtec competition.

115

00:08:47,770  -->  00:08:53,630
That is one of the operands is a box but Amodio Well the other is a Plimmer dough in such as the box

116

00:08:53,690  -->  00:08:57,860
palmetto is first unboxed before the operation is applied.

117

00:08:58,360  -->  00:09:01,880
This applies to all other comparison operators too.

118

00:09:01,900  -->  00:09:06,240
In fact earlier you saw that this was also the case with our operators.

119

00:09:06,700  -->  00:09:11,420
Let's not look at this interesting example just taken from the effective Daleiden.

120

00:09:11,590  -->  00:09:12,710
Here we have a variable.

121

00:09:12,740  -->  00:09:18,380
I of byte integer plus knowing the method name does unbelievable.

122

00:09:18,610  -->  00:09:21,260
I is being compared with the ground zero.

123

00:09:21,820  -->  00:09:23,700
Can you guess what would happen here.

124

00:09:24,160  -->  00:09:25,480
Billett been the best.

125

00:09:25,510  -->  00:09:27,610
Weird it doesn't.

126

00:09:27,700  -->  00:09:30,610
It throws the EDR null pointer exception.

127

00:09:31,180  -->  00:09:35,940
This is because as mentioned earlier in this expression I equal to zero.

128

00:09:36,130  -->  00:09:41,980
Since I is a box medu the operator will first try to unbox it.

129

00:09:41,980  -->  00:09:45,370
That is it would invoke the value method on it.

130

00:09:45,850  -->  00:09:52,720
However I as an on here as it is not explicitly initialized and we know that invoking a method on a

131

00:09:52,720  -->  00:09:56,550
number of friends would throw a null pointer exception.

132

00:09:56,800  -->  00:10:03,910
So always keep in mind that a box perimeter is initialized with null by default as it is an object like

133

00:10:04,000  -->  00:10:06,430
any other regular object.

134

00:10:06,430  -->  00:10:13,060
Let's actually go ahead and look at this unbelievable example in code.

135

00:10:13,090  -->  00:10:14,090
Here is the method.

136

00:10:14,140  -->  00:10:15,550
Unbelievable.

137

00:10:15,850  -->  00:10:19,620
And here is the static variable which is initialized or NULL.

138

00:10:19,950  -->  00:10:25,240
And this is the expression I equals to zero is true is a null pointer exception.

139

00:10:25,240  -->  00:10:28,390
So let's just go ahead and run this program iron.

140

00:10:28,410  -->  00:10:29,760
Here it is we are in Woking.

141

00:10:29,770  -->  00:10:31,710
Unbelievable from the main.

142

00:10:32,290  -->  00:10:33,360
It's already compiled.

143

00:10:33,370  -->  00:10:35,420
So let me just run this.

144

00:10:36,250  -->  00:10:42,430
So here we are getting a null pointer exception at line 294 where we have the expression equals to zero

145

00:10:42,430  -->  00:10:43,300
.

146

00:10:43,330  -->  00:10:44,680
So just go ahead.

147

00:10:44,690  -->  00:10:47,200
Now you can download the program BASIX demo.

148

00:10:47,380  -->  00:10:55,120
It has the better methods or the three methods that you have seen in this lesson and that's it.

149

00:10:55,600  -->  00:10:56,990
So that's about it.

150

00:10:57,010  -->  00:11:04,360
Just to summarize an identity comparison is done if both operands of equal two are not equal to operators

151

00:11:04,480  -->  00:11:12,640
are box primitives for all other operators unboxing is FOSS performed similarly if only one of the operands

152

00:11:12,640  -->  00:11:18,510
is a box Belmondo then unboxing is first performed regardless of the operators.

153

00:11:18,640  -->  00:11:26,490
That is operettas can be even equal to or not equal to engender use primitives in preference to baulks

154

00:11:26,500  -->  00:11:27,210
primitives.

155

00:11:27,280  -->  00:11:33,820
Whenever you have the choice if you have to use box primitives then exercise care due to the issues

156

00:11:33,820  -->  00:11:35,280
we are discussing in this lesson.

157

00:11:35,560  -->  00:11:35,980
Thank you
