WEBVTT

00:00.650 --> 00:01.130
Hi everyone.

00:01.130 --> 00:02.390
Welcome back to this tutorial.

00:02.390 --> 00:09.600
In this tutorial we will code and explore the boxing and unboxing operations using numeric frames.

00:09.620 --> 00:14.720
I would like to give you a general idea about what is boxing and unboxing first and then we will go

00:14.720 --> 00:17.900
and explore it using our examples.

00:18.750 --> 00:19.560
What is boxing?

00:19.590 --> 00:23.820
Boxing is nothing but converting a primitive type to wrapper.

00:23.820 --> 00:24.430
Class type.

00:24.450 --> 00:25.980
Let's take a look at an example.

00:26.280 --> 00:31.590
The example is converting an int primitive type to an appropriate integer wrapper.

00:31.590 --> 00:32.320
Class type.

00:32.340 --> 00:33.600
That's called boxing.

00:33.630 --> 00:35.760
Let's talk about what is unboxing.

00:36.830 --> 00:41.720
Converting a wrapper class type to a primitive type is called unboxing.

00:41.960 --> 00:43.430
Let's take a look at an example.

00:43.460 --> 00:47.990
The example is converting an integer wrapper class type to int.

00:48.020 --> 00:49.400
Primitive type.

00:49.400 --> 00:50.930
Let's go ahead and code this one.

00:53.580 --> 00:56.980
I'm going to create a class in the numeric streams package.

00:57.000 --> 01:05.130
The class name is going to be numeric streams Boxing.

01:07.060 --> 01:09.220
Unboxing example.

01:10.930 --> 01:14.380
Let's make this class executable by adding public static void main.

01:17.690 --> 01:18.260
The first service.

01:18.260 --> 01:21.620
I'm going to create a method called boxing.

01:22.880 --> 01:26.930
Public static void boxing.

01:28.070 --> 01:30.890
So we all know the definition of boxing.

01:30.890 --> 01:36.530
Boxing is a process of converting an int to an integer wrapper class type.

01:36.560 --> 01:43.400
What I'm going to do is I'm going to make use of the int stream dot range clause.

01:43.400 --> 01:45.860
I'm going to give the number as 1 to 10.

01:46.070 --> 01:52.400
Basically this is going to create an int stream of ten elements and I'm going to use a handy method

01:52.400 --> 01:55.190
which is part of the stream, which is called boxed.

01:55.220 --> 01:57.200
This is going to create.

01:58.420 --> 01:59.950
A wrapper class type.

01:59.950 --> 02:07.090
Basically the input is going to be a type int and this is going to give you an output of type integer.

02:08.170 --> 02:17.230
So the conversion of int to the primitive, primitive, int to the wrapper class type happens using

02:17.230 --> 02:18.640
the boxed method.

02:18.640 --> 02:22.930
And then what I'm going to do, I'm going to collect collectors.

02:24.570 --> 02:25.830
Dot to list.

02:27.020 --> 02:28.880
So this is going to collect.

02:29.800 --> 02:31.930
All the integers to a.

02:33.090 --> 02:35.310
List of integer type.

02:35.910 --> 02:37.370
So I'm going to return this.

02:37.380 --> 02:41.070
This one is going to be list of integers.

02:42.900 --> 02:44.220
Okay, let's import this.

02:44.250 --> 02:45.030
There you go.

02:45.120 --> 02:47.220
Let's call this method and then check what happens.

02:48.040 --> 02:48.720
This out.

02:51.380 --> 02:51.740
Boxing.

02:51.740 --> 02:56.990
I'm going to name this as boxing and call the boxing method.

02:58.670 --> 02:59.570
Run this one.

03:02.020 --> 03:02.560
There you go.

03:02.590 --> 03:13.420
It created the list of integer type with ten values, because here the int stream iterates for ten elements.

03:13.420 --> 03:20.440
Basically, this will create an int stream of ten elements.

03:21.190 --> 03:21.730
Sounds good.

03:21.760 --> 03:25.270
Let's take a look at the next example which is unboxing.

03:25.540 --> 03:32.560
I'm going to create a method called public static void unboxing.

03:34.330 --> 03:39.730
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

03:39.730 --> 03:40.450
input.

03:40.480 --> 03:44.470
The input is going to be of type list of integer.

03:44.470 --> 03:45.490
An integer list.

03:45.490 --> 03:46.690
So unboxing.

03:46.720 --> 03:52.270
We know the definition unboxing is going to convert a wrapper class type to primitive type.

03:52.270 --> 03:52.930
Right?

03:53.080 --> 03:57.850
So wrapper to primitive.

03:58.360 --> 03:59.650
That's what we are going to do.

04:00.870 --> 04:02.190
You already have the wrapper type.

04:02.400 --> 04:04.260
Next thing is what we are going to do.

04:04.290 --> 04:09.570
We are going to do integer list, dot stream.

04:09.960 --> 04:13.350
There is a handy method called map to int.

04:13.380 --> 04:14.910
As you see here, map to int.

04:14.940 --> 04:21.030
What this is going to do, this is going to I'm going to create.

04:22.490 --> 04:24.500
Integer, colon, colon int value.

04:24.500 --> 04:35.630
So here this integer is going to give us the wrapper class type wrapper integer values integer values

04:35.630 --> 04:41.480
one by one, and using this int value is going to give you the int value, which is a primitive type.

04:41.480 --> 04:48.020
Here is where we are converting the integer value to this value, this map to int what it is going to

04:48.020 --> 04:48.470
do.

04:48.470 --> 04:51.440
This is going to give you an int stream back.

04:51.440 --> 04:55.910
So what I'm going to do using that int stream, I'm going to perform.

04:58.400 --> 04:58.760
Right.

04:58.760 --> 05:02.490
If I perform some, it is going to give me the total of this value.

05:02.510 --> 05:06.380
I'm going to return this and then I'll make this.

05:06.410 --> 05:15.050
INT So let's put this, make sure we have the appropriate text here and boxing.

05:15.050 --> 05:16.100
So what is the method?

05:16.400 --> 05:17.870
Unboxing is a method.

05:18.050 --> 05:18.710
Okay.

05:18.710 --> 05:20.570
And the next step is we need the input, right?

05:20.570 --> 05:24.260
So because this accepts an input of type list of integers.

05:24.260 --> 05:26.540
So we just coded as part of the boxing.

05:26.540 --> 05:29.420
Boxing is return you returning you the list of integers.

05:29.420 --> 05:30.260
So.

05:31.170 --> 05:32.110
How can we do this?

05:32.130 --> 05:39.480
We can do list of integer can name this one as integer list.

05:39.990 --> 05:45.990
And then if you call the boxing, that is going to give you the input list and pass this input list

05:45.990 --> 05:47.460
as an input to this.

05:47.640 --> 05:50.460
Basically, this is going to perform.

05:51.370 --> 05:57.580
The iteration here using the stream and then we are converting that integer value.

05:59.010 --> 06:04.830
Integer wrapper value to the actual primitive type, but here we are using mapped int, which eventually

06:04.830 --> 06:08.280
gives you the int stream and using that int stream.

06:08.280 --> 06:15.750
We are performing a summation here, so this returns in stream and the input is going to be the int

06:15.750 --> 06:19.230
value of the wrapper class.

06:22.790 --> 06:24.530
Let's run this and then check the result.

06:26.690 --> 06:27.190
There you go.

06:27.200 --> 06:28.780
It printed the value as 52.

06:28.790 --> 06:33.290
If you add all these value, that will give you the result as 55.

06:33.320 --> 06:38.780
So this is all about boxing and unboxing operations using the int stream.

06:38.780 --> 06:40.820
With this we came to the end of this tutorial.

06:40.820 --> 06:41.960
Thank you for watching.
