Full Transcript

·YouTLDR

Java Wrapper Classes Full Tutorial | Boxing, Unboxing, parseInt(), valueOf(), MAX_VALUE & More

15:11EnglishTranscribed Jul 24, 2026
0:00

Hello and welcome to my channel the

0:02

curious coder. Today we'll be talking

0:04

about a very simple yet important Java

0:07

topic wrapper classes in Java. Now it's

0:10

a basic concept but trust me a lot of

0:13

questions are asked around this in

0:14

interviews and the problem is students

0:17

often don't know what exactly to

0:18

prepare. Okay. So in this video I'll

0:21

tell you what all you should know under

0:23

wrapper classes and some important terms

0:26

like autoboxing and unboxing that you

0:28

must keep in mind before interviews.

0:31

Now before jumping into rapper classes

0:33

let me ask you a very simple question.

0:36

Is Java completely object- oriented? Yes

0:38

or no? So the answer is no. Because Java

0:42

contains primitive data types like bite,

0:45

short, int, long. Okay. And these are

0:47

not objects. They are simple values. So

0:50

technically Java cannot be called 100%

0:53

object-oriented.

0:54

Okay. But Java gives us a way to convert

0:57

these primitive data types into objects.

1:00

And that's exactly where wrapper classes

1:02

come in. So for every primitive data

1:05

type there exists a corresponding

1:07

wrapper class. Okay. So for bite there

1:10

is a class bite with capital B.

1:13

Similarly for short there is another

1:15

class short. For int there is integer.

1:18

For long there is long. For float there

1:20

is a class float with capital F.

1:22

Similarly for double also there is a

1:24

class double. For care there is

1:26

character. And for boolean there is

1:28

boolean. Okay. Now the obvious question

1:32

if primitives already work fine then why

1:34

do we need wrapper classes? We need them

1:37

because some features in Java like

1:39

collections, generics or some method

1:41

parameters do not accept primitives.

1:44

Okay. They only work with objects. Also,

1:46

wrapper classes have some utility

1:48

methods inside them which we use a lot

1:50

in our daily coding life. Okay. So,

1:53

let's look at these classes in the code

1:55

and understand what all features they

1:57

have to offer. So, let's first start

1:59

with how to convert a primitive data

2:02

type into an object. Okay. So, let's say

2:05

I have an int variable int a is equal to

2:08

10 and I want to convert this primitive

2:11

data type into an object. Okay. So, how

2:14

can I do this? I'll use the wrapper

2:16

class integer.

2:18

I'll write integer obj. I'll try to

2:20

create an object of this class and for

2:22

that I will use this method integer dot

2:26

value of. Okay. And here I will pass

2:28

this variable a like this. All right. So

2:33

value of is a static method in this

2:36

integer class. It takes primitive as the

2:38

argument and it returns the integer

2:41

object. Okay. So this is how we can

2:43

convert a primitive data type into an

2:46

integer class basically into a into its

2:48

wrapper class. Okay. So this process is

2:51

called boxing. Okay. You should know

2:54

this term because sometimes in interview

2:56

they directly just ask you that what is

2:58

boxing and what is unboxing. And if you

3:00

don't know this term then you might get

3:02

lost like what they're trying to ask.

3:04

But in reality you can see that it's a

3:06

very simple concept. Okay. Just

3:08

converting a primitive data type into an

3:10

object. All right. So this is called

3:12

boxing and you can see that there is a

3:16

warning that is there that says

3:17

unnecessary boxing. Okay. So why is

3:20

that? Because instead of doing it like

3:23

this we we could have done it in a

3:25

simpler way and that is

3:28

integer obj let's call it obj 1 is equal

3:32

to a simply like this. Okay. So the

3:36

compiler converts the primitive data

3:38

type into its object automatically.

3:40

Okay. Okay, it automatically performs

3:42

this boxing process and that is why this

3:45

is called autoboxing.

3:47

All right, so when you call the integer

3:49

do value of and you convert it like this

3:52

then it's called boxing. But if you

3:53

directly just write the variable here

3:55

and convert it into its object then it's

3:58

called autoboxing.

4:00

Okay. Now let's say you want to convert

4:03

it back to the primitive data type. All

4:06

right. So how can we do that? Again that

4:09

is also very simple. What I'll do is

4:11

I'll just take another variable int x

4:14

let's say and uh this one is the uh

4:18

integer variable integer object. So I

4:20

will just call this method int value.

4:25

All right. So this is another static

4:27

method uh not a static method this is

4:29

another method in this integer class

4:31

which you can call using the object of

4:34

this class. So I just call this method

4:36

and it returns a primitive data type.

4:38

Okay, it returns the primitive value of

4:40

your object.

4:42

All right, so you can save this in this

4:45

primitive variable. Okay, now this

4:48

process is called unboxing.

4:50

Okay, so like this was boxing and this

4:52

is the reverse of it. So it's called

4:54

unboxing and like this was manual boxing

4:57

and this was autoboxing. Similarly, this

4:59

is manual unboxing. All right. Now again

5:03

if you could guess it right you can see

5:05

that we can directly also convert it

5:08

like this in y is equal to obj1 okay we

5:11

didn't need to call this method compiler

5:14

will do it on its own and this process

5:17

is called auto unboxing okay so we have

5:21

boxing autoboxing unboxing and auto

5:25

unboxing

5:27

all right so now let's see some of the

5:29

utility methods that I was talking

5:31

route. These are some methods that we

5:34

use in our day-to-day coding and uh

5:36

these are very helpful to us. Okay. So,

5:39

first let's say we have a string.

5:42

Let's say string number is equal to 10.

5:49

Okay, it's a string value and I want to

5:52

convert this string value into an

5:53

integer value. All right, so how can I

5:56

do that? So for that we have a utility

5:58

method. basically a method inside

6:00

integer class integer dot value of.

6:03

Okay, so as you can see we used value of

6:05

here. So in value of we can pass a

6:07

primitive data type and we can also pass

6:09

a string value. Okay, so I will use this

6:12

one string value and I will pass number

6:14

over here and this method will return me

6:18

an integer object. Okay, so this is how

6:20

we can convert a string into an integer.

6:23

All right, so I'll just save it in some

6:25

variable. Let's say OPJ2

6:29

like this. Okay. Similarly, we also have

6:33

a method

6:34

parse int. Okay. This also converts a

6:38

string into integer. And what it does is

6:40

the difference is that it will convert

6:43

it into a primitive data type. Okay.

6:46

Let's call it int z. Yeah. So this is

6:49

also a method inside integer class. It

6:51

takes string as the parameter but it

6:53

converts it back into int. Okay. not

6:56

integer. All right, so these are two

6:59

methods. Then we have int value as we

7:02

already saw that it converts the object

7:05

into its primitive data type. All right,

7:08

so this is also another method inside

7:09

this class. Then let's say we want to

7:14

convert an integer into a string. Okay,

7:17

so we have this integer OBJ2 and let's

7:19

say I want to convert it into a string.

7:21

So for that also we have a method which

7:24

is two string method. All right. So this

7:27

method will convert it back into a

7:30

string. So any integer can be converted

7:32

into a string like this. Very simple

7:34

process. Okay. So string number one is

7:38

equal to

7:40

let's call this yeah

7:42

okay. So string number one is equal to

7:44

obj2.2 string. All right. So we had this

7:47

object and using this object we called

7:49

the tworing method and it got converted

7:52

back to string. All right. Now you can

7:54

observe that like there was this zed as

7:56

well. And if I try to call zed dot

7:58

tworing it won't work. Okay. Because

8:01

this zed is a primitive data type. It's

8:03

a primitive variable. Right? Because int

8:05

is a primitive data type. But for obj2

8:08

it will work because it's an object.

8:10

Okay. So this is the benefit of using

8:13

these wrapper classes. they have certain

8:15

methods which we can use which help us a

8:18

lot. Okay, it's so easy to convert it

8:20

into string and convert it back into

8:21

integer. All right, then we have some

8:24

more methods as well. There is a compare

8:27

to method. Okay, so let's say I'll just

8:30

remove all this. It's a bit chaotic now.

8:33

I'll simply take

8:38

like this integer. Let's say there are

8:45

two integer variables. Integer I1 is

8:49

equal to this and integer

8:54

I2 is equal to and let's not even call

8:56

integer do value of let's just use them

8:58

directly. Okay. 10 and 20. We'll do the

9:01

autoboxing. All right. So we have two of

9:04

them. Now there is a method compare to

9:07

what it does. it you can just call I1

9:10

dot compare to and in the argument you

9:12

will pass another integer okay so it's

9:16

basically comparing these two integers

9:18

with each other so let me just print

9:20

this system outprint ln yeah yeah

9:26

all right so you will see that

9:29

it returns minus one okay so this means

9:32

that i1 is less than i2 because we

9:34

called it like this i1 dot compared to

9:36

i2 so minus1 means that I1 is less than

9:38

I2. Let's say it would have been 30.

9:43

Now it's returning one. This means I1 is

9:46

greater than I2. Okay. And let's say

9:49

both are 20.

9:52

Now it will return zero. So zero means

9:54

that both are exactly the same. Okay. So

9:58

let me make it 10 again. Yeah.

10:00

Similarly, we have I1 dot uh we have a

10:05

sum method as well. Okay. And we have

10:07

max and min methods as well. Let me show

10:10

all these one by one. First let me call

10:12

integer domax. So I can pass these two

10:15

in it I1 and I2. And let me print this

10:21

turn.out.print

10:22

ln.

10:25

So it will just guess it will just tell

10:28

you which one is the maximum among these

10:30

two. Okay. So if I call this great. So

10:34

this returns minus one and this will

10:36

return 20 because 20 is the greater one

10:38

among these two. Okay. Similarly

10:45

we also have integer dot min and this

10:48

will tell you the minimum one. All right

10:50

which is 10. And we can also use integer

10:55

dot sum and this will just add these two

10:59

integer values and you will get the

11:01

result. All right. So we have these many

11:04

utility methods inside it and we also

11:07

have some fields inside it like I'll

11:10

show you we have integer dot

11:14

max value okay this is a static final

11:18

field in this class integer and this is

11:21

the maximum value of which is there in

11:23

the integer which is present in in any

11:26

integer. Okay, you can see that max

11:28

value is this. So you might have used

11:31

this a lot when you try to solve your

11:32

data structure problems. You you might

11:34

have seen this integer domax value and

11:36

similarly we have integer dot min value.

11:40

So these are basically

11:42

predefined and you can directly use

11:44

them. Okay. Min value is the least value

11:47

which is there. Okay. Minus 214. So let

11:50

me print these as well.

11:53

Okay. So this is the max value of an

11:55

integer and this is the minimum value of

11:57

an integer. All right. So these are some

12:00

important methods, some utility

12:02

functions and utility variables that are

12:04

there in this class and we often use

12:06

them daily. Okay. So that's why Java has

12:08

provided a wrapper class as well so that

12:11

it's easy for us to write code. Okay.

12:14

And there was one more reason at some

12:17

places you cannot even use int directly.

12:19

Okay. There they have not allowed you uh

12:21

you to use int. Okay. For example inside

12:24

a list. Let's say if you want to create

12:26

a list. So this list list is equal to

12:30

new array list. Okay.

12:34

So here you have to tell that the which

12:38

what is the type of this list. Okay. So

12:40

let's say you want to create a list of

12:42

int. Okay. List of an integer. So if

12:44

you'll try to give it like this, it

12:46

won't work. Okay. It will give an error

12:48

that type argument cannot be of a

12:50

primitive type. Okay. So here you will

12:53

have to use integer.

12:55

All right. like this.

12:58

See there is no error now. Okay. So that

13:01

is why at some places in Java there is

13:03

mandatory to use these wrapper classes.

13:06

Hence that is also one more reason for

13:08

having these classes. Okay. And let's

13:10

say if I add some values

13:15

place dot add to okay. So you can see

13:19

that I am directly adding int values

13:21

here. Okay. I'm not add I'm not creating

13:23

an integer object and adding it

13:25

specifically. I'm just adding int

13:28

values. So actually whenever you do list

13:30

dot add again Java does autoboxing.

13:34

Okay. It automatically does the boxing

13:36

and it automatically converts this int.

13:41

Okay. And let there is another example.

13:43

Let's say we have a set. So here also if

13:46

I'll try to have an int value like this.

13:50

Okay. new hash set

13:53

it won't work okay because again

13:56

primitive primitive data type does not

13:58

work here all right so here also I will

14:00

have to take it like integer and by the

14:03

way these are the generics like these

14:06

two arrows this and this these are

14:08

called generics so if you go to the list

14:11

interface here they have defined these

14:13

generics so in these generics you have

14:15

to pass what is the type of the list

14:17

okay whether it's a list of integer or a

14:19

list of double or a list of string. So

14:22

in these generics as I was saying you

14:24

cannot use primitive data types. Okay.

14:28

So in set also you will see that you

14:30

will have to use integer and if you'll

14:33

add something here again you can

14:35

directly add an int value. Compiler will

14:38

automatically do the boxing logic. Okay.

14:41

So I hope you understood what rapper

14:43

classes are and as you can see it's a

14:46

very simple topic but still it's asked a

14:49

lot in interviews and if you have not

14:51

heard about rapper classes or this

14:53

boxing autoboxing unboxing then you

14:55

might not be able to answer this

14:57

question. Okay so I hope you like the

14:59

video and if you want more videos on

15:02

Java core interview concepts then please

15:04

stay tuned and subscribe to my channel

15:06

the curious coder. Okay.

More transcripts

Explore other videos transcribed with YouTLDR.

Get the TLDR of any YouTube video

Transcribe, summarize, and repurpose videos in 125+ languages — free, no signup required.

Try YouTLDR Free