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