So in this video we're going to talk about the concept of auto boxing and unboxing.
So to start with we're just gonna have a little brief recap of arrays and
array lists and we'll move on to autoboxing.
So firstly, you're aware that we can do something like this, no doubt because
we've actually been through this with a code creating a regular array.
Let's just call that stringArray equals new String(10).
And likewise we could do that for
an integer if we wanted to new int(10) so that's creating array.
You've seen that before, I've ten elements starting from position,
element 0 through 9 and we can also create an array list.
We can do ArrayList String.
You've seen this before and
we'll call that string arrayList = new arrayList like so.
And we can use that and then we can do something like stringArrayList.add "Tim".
And that would add an element to the arrayList and we would actually add
an object of the type string to the arrayList for us automatically.
But interestingly enough, what we can't do is something like this.
We can't do an arrayList int
inarrayList = new ArrayList.
We actually get an error.
And the error we actually get is, type argument cannot be of primitive type.
And this, as it turns out, is an issue because a primitive type is not a class.
And we actually need that, a class reference here,
to be able to use that to be able to save something to an arrayList.
So, if we had a list of integers, we can't save them based on what we know so
far of Java, in an arrayList.
What we could do, for example, we could do something like this,
we could create a class.
I'm just going to create it in here, we can put create, and
we'll do type in class, and we'll call it intClass.
We can have a private int myValue.
We can add a constructor.
And we can also add a getter and setter.
Like so, very simple little class,
as you can see there, and we can do something like this.
So we can just comment this line out for now.
And we could do something like arrayList
= intClass = intClassArrayList
= new ArrayList like so.
And then we could do intClassArrayList.add.
So we could do something like new IntClass(54) and
what that would do would be in the first array element in the array list.
It would actually add an object of type IntClass with the value 54 and
obviously this would the class.
This would be the class that the objects being created from that would be added to
the arrayList, so that would work.
It's a little bit messy because there's all this extra constructor code and
there's getters and setters and so forth that we'd need to create to do that.
So wouldn't it be good if there was an easier way to do this and
for all the primitive types, the variables,
the data types, if there was a way to actually use those as objects.
Well that's what autoboxing is all about because Java does provide us
with that functionality.
Now, you might be looking at the string and saying to yourself, well hang on,
string's a primitive type, why isn't that working?
Why is that working you should say.
Well if you recall, String in fact is not a primitive type.
It's actually a class in its own right.
And usually, you can actually tell that.
On the Mac, I'm gonna hold down Ctrl and hover over it and click.
And you can see that String is actually a class in its own right.
It's actually fully a class, and it's got all these other bits and
pieces in it, and this is some of the Java source code relating to a class.
So that's the reason why that is working,
and that is because that's actually a class in its own right.
Now contrasting that to going back to our main again, and
if we try to do the same thing to integer, I'm holding command on my Mac.
And I can't click on that.
I can hover over the string.
But can't have the integer.
I can hover over the integer, but nothing happens when I go to click it.
Hover and that's because it isn't a class.
So the bottom line is for the arrayList but also for lots of these classes and
lots of other Java classes, it needs to be passed a class.
In other words, passed an object.
So it's very common for you to want to use these primitive types, and
to be able to pass those as classes.
So again, you could do something like this example up here,
where you could create your own class and parse it, and yes, that will work.
That's not really what we want to do.
There's actually a better way of doing that and
we can use the concept of autoboxing.
And it turns out that Java supports some primitive types by using a class,
an object wrapper if you will, for the class because if you think about it,
what we've done with this class here that we created.
We actually created a wrapper, so in other words we wrapped and we sort of said
the integer myValue is the variable that's gonna contain the value for this class.
And we wrapped up in a class.
We made sure that it wasn't publicly available with a variable.
We can only update the variable using a constructor initially and then a setter.
And you can retrieve the value only by using the getter.
So that's sort of the concept of a wrapper.
Where you're actually wrapping this functionality and
the way to access this is the access modifier.
So that not everyone can access it.
Well it turns out that Java's got a lot of these in built.
So for example in the case of Integer, we can do something like this.
We can put Integer, and
we can put integer = new Integer.
And you can see that the default option for
a new Integer when we're creating a new object, we can actually pass an int value.
So if we do something like that, and the difference between Integer and
int is that Integer's actually a class.
So again, if we hover over this we can see that I can actually click on that.
And we get the fact that it's actually a class.
And all this other stuff relating to how it's set up within Java.
So you'll find that there's one of those, one of these for each primitive type.
So there's one there for an int.
Which will see is called the Integer.
There's a Double.
I wanna do a couple.
I won't do all of them.
But the other one's called Double.
We could call that doubleValue because I can't use the word Double
cuz it's already keyword = new Double.
And if you hover over you can see it's hunting for doubleValues so
we can put something like (12.25) and that will quite happily accept that as a value.
So that helps us in a sense that we can now use in array list.
So we can do something like arrayList
We can call it intArrayList = new arrayList.
And you can see now Java's quite happy with that because this is actually a valid
class unlike what we were trying to do up here earlier when we were trying to
actually assign a primitive type into it.
So let's just use it as an example just to see actually how it works.
Let's now we've created our Integer class,
let's just do a full loop and actually add some entries.
So a typical way to add them would be something like for(int i=0; i<=10; i++).
We could put intArrayList oops, intArrayList .add.
And noticing here that it's wanting an Integer now.
This is the one we're going to select.
The integer is the type for this variable intArrayList so I'm gonna add or
we could do something like (Integer.valueOf i).
And what the .valueOf is actually taking the value of i as the primitive type and
converting it automatically into the Integer class I should say so
that we can use it as a class.
Therefore then when we come to execute this, we can do something like,
so we'll just copy that code.
And need that first line and we'll do something like (i + let's
just output the contents, and it will be intArrayList.get,
the element that we want to retrieve and .intValue.
And notice there's a int value which is a value which converts it back from Integer
from the Integer class back into an int type to a primitive type.
Again, that's how we convert it.
So essentially the two parts that we're dealing with here is this part here,
this is auto boxing.
This is when we're creating or converting an integer,
a base primitive type int to an Integer, that's called auto boxing.
So we're taking the primitive type and we're going to the object wrapper.
We're going to the class, that's called autoboxing.
Unboxing is where we're taking it from the class or from the object wrapper and
converting it back to a primitive type.
So that's the difference between autoboxing and unboxing.
So looking at that code, you can see that it's pretty straightforward and
if we run that, we should find that we get the results that we think we'll get.
We should just get the output to show to those integers are being saved.
And we can see we get 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
10 as we would expect based on the code that we have developed.
So nothing particularly hard there, but
it turns out that Java actually even makes things easy for you.
I wanted to show you the long way of doing it first.
And now I want to show you the short way, cuz it turns out that it's actually easier
than you think to create some code to convert to using this autoboxing and
unboxing.
So for example what we can do is something like this Integer myIntValue = 56.
So looking at that, you'll probably think well hang on, why is that working,
because surely that's incorrect.
Because we haven't got any reference that we have up here Integer = new.
So that when we're creating a new class you would normally have to use the new key
word and then the name of the class and then pass in the parameters for
the constructor.
Well it turns out that Java is helping you out here by issuing a bit of a shortcut.
So when you actually type that command in what happens is when the code is compiled
this code will be executed.
So the Integer.value(56) I think I typed.
So that's about what happened at compiled time.
So you can type it like this but at compile time, it gets converted to that.
In other words Java's doing the hard work for you.
Because it recognizes and it says okay this is an integer, an integer object so
this is a class and you entered an integer here, so
therefore I know what to do with it.
I'm gonna actually use this reference here to actually create the proper
class based on the proper object, in this case, myIntValue.
Now if you typed something like 56.56,
that's different because you've now typed a Double.
And it's actually saying to you there it's looking for
an integer and you've typed in a Double.
To get that to work, you'd actually have to use a Double class,
the object wrapper Double to get that to work.
And the other thing I want to show you also is,
if I just go back to make that valid again.
We can then type int myInt = myIntValue.
And again looking at that you'll say well hang on, this is a primitive type and
this is an object.
How is that working?
Well what's actually happening is Java's actually doing something like this.
It's actually putting Integer, or it's actually putting myInt.intValue.
It's putting that code in for us automatically.
So in other words, if we put that code, copy that and we paste it in there, that's
essentially what it's actually doing is oops, that was meant to be myIntValue.
That's actually what it's doing.
Fix that up.
So basically it's looking at myIntValue and saying, well that's an Integer,
an object wrapper.
And it's actually putting the .intValue in.
If we have a look at the method .intValue, you can see that returns an int.
So that's why that's working for us.
So this is the concept of autoboxing and unboxing.
And you can see that Java is trying to make things easier for you by actually
at compile time changing the code that you are typing to what the correct code is for
it to work.
And preventing compile time errors that come up, and
really just generating some of the code for you.
So it's important to understand that what's happening behind the scenes.
Cuz otherwise you'll look at the code and think well this makes no sense at all.
So there's nothing stopping you from doing things the long way.
Doing something like that perhaps to create a new Double or to use in the case
that I showed you up here, we can actually type Integer.value(56).
If you type it like that, that would certainly work as well but
just probably with these days with a modern compilers it's best to actually use
it this way because it's less code.
Easier to read, and just makes a lot of sense.
So that's just to show you that this works.
Instead of using integers, because we've used those a lot in previous examples,
let's do something with a Double type, just so it makes sense in a loop.
So we can put something like for.
Actually what we'll do first is we'll create an arrayList.
So it's arrayList Double.
We'll put myDoubleValues = new arrayList = new arrayList=DoubleValues.
Now we're gonna put something like for(double we'll call it dbl,
our variable, dbl equals we'll start on 0.0.
Then we'll put something like all dbl>=10.0.
Again, we're putting the decimal points because it's a double.
We wanna increment double += 0.5.
So we're gonna increment it by0.5 each time we go through the loop starting at
0.0 until we get to 10.0.
So with all that boxing we can type the name of the array,
so that's myDouble Values.add, then we can put Double.value of dbl.
And that would certainly work and then we could actually use that loop again.
What we'll do is a different loop this time.
We'll actually iterate through the entire array so
we'll use an integer this time i=0, i < and
it's gonna be myDoubleValues.size i ++.
And we're gonna put double value = myDoubleValues.get(i).,
this time it's gonna be .doubleValue, because we're converting to a double, and
you can see on the right over here it's actually going to be returning a double.
So that's correct.
So that's gonna return that as a double, and then we can actually print that out.
We could put something like i + value.
So that's one way of doing it.
So the first example, double.valueOf, that's autoboxing.
We're converting the primitive type double to the object wrapper,
to the object in other words, that's autoboxing.
And here we're using the .doubleValue so we're actually unboxing,
we're converting it from the object.
The object wrapper back into the primitive top of
this case the double which is value.
So let's just run that, make sure it works,
then we'll just do the shortcuts to this code again.
And I've got a typo here, should be an i = like that and
we'll just try running them again Okay, so that's better and
what I might do just to make things a little bit easy to read, let's just
get rid of those from the output so that we don't see that in the output.
Then we run it again and there's our example.
Starting from 0, we've incremented by 0.5 each time, 0.5, 1,
1.5, right through to 10, and there's 20 elements.
So again, that's the autoboxing and unboxing.
But equally, as I mentioned, we could also use the shortcut.
So the shortcut would actually be to remove this altogether, and just add dbl.
And likewise for this, we could do the same thing.
We could actually get rid of this .doubleValue if you wanted to,
and the code will still compile and run.
And the reason that's running is behind the scenes.
Java is actually adding those commands using the dot value of, and
the dot, in this case doubleValue to actually retrieve those values back and
convert them for us automatically.
So that's auto boxing, and unboxing, good concept to understand when you need to
actually store the common primitive types.
The primitive data types that are part of Java in array lists or
pass them as parameters using objects, especially to some other Java
code that's out there these days that will actually require you to use these.
So they are used quite a bit.
The object wrapper equivalence for the primitive type, so
it's certainly good to know those.
And just in finishing off, as I mentioned there's one of these for
each primitive type so you can probably find those easily enough.
So I'm going to end the video here.
In the next video I'm going to show you the challenge for this part of the course.
And type that out and then you can have a go at that.
So I'll see you in that next video.