Hi everyone.
Welcome back to this tutorial.
In this tutorial we will code and explore the boxing and unboxing operations using numeric frames.
I would like to give you a general idea about what is boxing and unboxing first and then we will go
and explore it using our examples.
What is boxing?
Boxing is nothing but converting a primitive type to wrapper.
Class type.
Let's take a look at an example.
The example is converting an int primitive type to an appropriate integer wrapper.
Class type.
That's called boxing.
Let's talk about what is unboxing.
Converting a wrapper class type to a primitive type is called unboxing.
Let's take a look at an example.
The example is converting an integer wrapper class type to int.
Primitive type.
Let's go ahead and code this one.
I'm going to create a class in the numeric streams package.
The class name is going to be numeric streams Boxing.
Unboxing example.
Let's make this class executable by adding public static void main.
The first service.
I'm going to create a method called boxing.
Public static void boxing.
So we all know the definition of boxing.
Boxing is a process of converting an int to an integer wrapper class type.
What I'm going to do is I'm going to make use of the int stream dot range clause.
I'm going to give the number as 1 to 10.
Basically this is going to create an int stream of ten elements and I'm going to use a handy method
which is part of the stream, which is called boxed.
This is going to create.
A wrapper class type.
Basically the input is going to be a type int and this is going to give you an output of type integer.
So the conversion of int to the primitive, primitive, int to the wrapper class type happens using
the boxed method.
And then what I'm going to do, I'm going to collect collectors.
Dot to list.
So this is going to collect.
All the integers to a.
List of integer type.
So I'm going to return this.
This one is going to be list of integers.
Okay, let's import this.
There you go.
Let's call this method and then check what happens.
This out.
Boxing.
I'm going to name this as boxing and call the boxing method.
Run this one.
There you go.
It created the list of integer type with ten values, because here the int stream iterates for ten elements.
Basically, this will create an int stream of ten elements.
Sounds good.
Let's take a look at the next example which is unboxing.
I'm going to create a method called public static void unboxing.
And in here what we are going to do, we are going to pass in an input, so I'm going to pass in an
input.
The input is going to be of type list of integer.
An integer list.
So unboxing.
We know the definition unboxing is going to convert a wrapper class type to primitive type.
Right?
So wrapper to primitive.
That's what we are going to do.
You already have the wrapper type.
Next thing is what we are going to do.
We are going to do integer list, dot stream.
There is a handy method called map to int.
As you see here, map to int.
What this is going to do, this is going to I'm going to create.
Integer, colon, colon int value.
So here this integer is going to give us the wrapper class type wrapper integer values integer values
one by one, and using this int value is going to give you the int value, which is a primitive type.
Here is where we are converting the integer value to this value, this map to int what it is going to
do.
This is going to give you an int stream back.
So what I'm going to do using that int stream, I'm going to perform.
Right.
If I perform some, it is going to give me the total of this value.
I'm going to return this and then I'll make this.
INT So let's put this, make sure we have the appropriate text here and boxing.
So what is the method?
Unboxing is a method.
Okay.
And the next step is we need the input, right?
So because this accepts an input of type list of integers.
So we just coded as part of the boxing.
Boxing is return you returning you the list of integers.
So.
How can we do this?
We can do list of integer can name this one as integer list.
And then if you call the boxing, that is going to give you the input list and pass this input list
as an input to this.
Basically, this is going to perform.
The iteration here using the stream and then we are converting that integer value.
Integer wrapper value to the actual primitive type, but here we are using mapped int, which eventually
gives you the int stream and using that int stream.
We are performing a summation here, so this returns in stream and the input is going to be the int
value of the wrapper class.
Let's run this and then check the result.
There you go.
It printed the value as 52.
If you add all these value, that will give you the result as 55.
So this is all about boxing and unboxing operations using the int stream.
With this we came to the end of this tutorial.
Thank you for watching.