Java Wrapper Classes Full Tutorial | Boxing, Unboxing, parseInt(), valueOf(), MAX_VALUE & More
Hello and welcome to my channel the
curious coder. Today we'll be talking
about a very simple yet important Java
topic wrapper classes in Java. Now it's
a basic concept but trust me a lot of
questions are asked around this in
interviews and the problem is students
often don't know what exactly to
prepare. Okay. So in this video I'll
tell you what all you should know under
wrapper classes and some important terms
like autoboxing and unboxing that you
must keep in mind before interviews.
Now before jumping into rapper classes
let me ask you a very simple question.
Is Java completely object- oriented? Yes
or no? So the answer is no. Because Java
contains primitive data types like bite,
short, int, long. Okay. And these are
not objects. They are simple values. So
technically Java cannot be called 100%
object-oriented.
Okay. But Java gives us a way to convert
these primitive data types into objects.
And that's exactly where wrapper classes
come in. So for every primitive data
type there exists a corresponding
wrapper class. Okay. So for bite there
is a class bite with capital B.
Similarly for short there is another
class short. For int there is integer.
For long there is long. For float there
is a class float with capital F.
Similarly for double also there is a
class double. For care there is
character. And for boolean there is
boolean. Okay. Now the obvious question
if primitives already work fine then why
do we need wrapper classes? We need them
because some features in Java like
collections, generics or some method
parameters do not accept primitives.
Okay. They only work with objects. Also,
wrapper classes have some utility
methods inside them which we use a lot
in our daily coding life. Okay. So,
let's look at these classes in the code
and understand what all features they
have to offer. So, let's first start
with how to convert a primitive data
type into an object. Okay. So, let's say
I have an int variable int a is equal to
10 and I want to convert this primitive
data type into an object. Okay. So, how
can I do this? I'll use the wrapper
class integer.
I'll write integer obj. I'll try to
create an object of this class and for
that I will use this method integer dot
value of. Okay. And here I will pass
this variable a like this. All right. So
value of is a static method in this
integer class. It takes primitive as the
argument and it returns the integer
object. Okay. So this is how we can
convert a primitive data type into an
integer class basically into a into its
wrapper class. Okay. So this process is
called boxing. Okay. You should know
this term because sometimes in interview
they directly just ask you that what is
boxing and what is unboxing. And if you
don't know this term then you might get
lost like what they're trying to ask.
But in reality you can see that it's a
very simple concept. Okay. Just
converting a primitive data type into an
object. All right. So this is called
boxing and you can see that there is a
warning that is there that says
unnecessary boxing. Okay. So why is
that? Because instead of doing it like
this we we could have done it in a
simpler way and that is
integer obj let's call it obj 1 is equal
to a simply like this. Okay. So the
compiler converts the primitive data
type into its object automatically.
Okay. Okay, it automatically performs
this boxing process and that is why this
is called autoboxing.
All right, so when you call the integer
do value of and you convert it like this
then it's called boxing. But if you
directly just write the variable here
and convert it into its object then it's
called autoboxing.
Okay. Now let's say you want to convert
it back to the primitive data type. All
right. So how can we do that? Again that
is also very simple. What I'll do is
I'll just take another variable int x
let's say and uh this one is the uh
integer variable integer object. So I
will just call this method int value.
All right. So this is another static
method uh not a static method this is
another method in this integer class
which you can call using the object of
this class. So I just call this method
and it returns a primitive data type.
Okay, it returns the primitive value of
your object.
All right, so you can save this in this
primitive variable. Okay, now this
process is called unboxing.
Okay, so like this was boxing and this
is the reverse of it. So it's called
unboxing and like this was manual boxing
and this was autoboxing. Similarly, this
is manual unboxing. All right. Now again
if you could guess it right you can see
that we can directly also convert it
like this in y is equal to obj1 okay we
didn't need to call this method compiler
will do it on its own and this process
is called auto unboxing okay so we have
boxing autoboxing unboxing and auto
unboxing
all right so now let's see some of the
utility methods that I was talking
route. These are some methods that we
use in our day-to-day coding and uh
these are very helpful to us. Okay. So,
first let's say we have a string.
Let's say string number is equal to 10.
Okay, it's a string value and I want to
convert this string value into an
integer value. All right, so how can I
do that? So for that we have a utility
method. basically a method inside
integer class integer dot value of.
Okay, so as you can see we used value of
here. So in value of we can pass a
primitive data type and we can also pass
a string value. Okay, so I will use this
one string value and I will pass number
over here and this method will return me
an integer object. Okay, so this is how
we can convert a string into an integer.
All right, so I'll just save it in some
variable. Let's say OPJ2
like this. Okay. Similarly, we also have
a method
parse int. Okay. This also converts a
string into integer. And what it does is
the difference is that it will convert
it into a primitive data type. Okay.
Let's call it int z. Yeah. So this is
also a method inside integer class. It
takes string as the parameter but it
converts it back into int. Okay. not
integer. All right, so these are two
methods. Then we have int value as we
already saw that it converts the object
into its primitive data type. All right,
so this is also another method inside
this class. Then let's say we want to
convert an integer into a string. Okay,
so we have this integer OBJ2 and let's
say I want to convert it into a string.
So for that also we have a method which
is two string method. All right. So this
method will convert it back into a
string. So any integer can be converted
into a string like this. Very simple
process. Okay. So string number one is
equal to
let's call this yeah
okay. So string number one is equal to
obj2.2 string. All right. So we had this
object and using this object we called
the tworing method and it got converted
back to string. All right. Now you can
observe that like there was this zed as
well. And if I try to call zed dot
tworing it won't work. Okay. Because
this zed is a primitive data type. It's
a primitive variable. Right? Because int
is a primitive data type. But for obj2
it will work because it's an object.
Okay. So this is the benefit of using
these wrapper classes. they have certain
methods which we can use which help us a
lot. Okay, it's so easy to convert it
into string and convert it back into
integer. All right, then we have some
more methods as well. There is a compare
to method. Okay, so let's say I'll just
remove all this. It's a bit chaotic now.
I'll simply take
like this integer. Let's say there are
two integer variables. Integer I1 is
equal to this and integer
I2 is equal to and let's not even call
integer do value of let's just use them
directly. Okay. 10 and 20. We'll do the
autoboxing. All right. So we have two of
them. Now there is a method compare to
what it does. it you can just call I1
dot compare to and in the argument you
will pass another integer okay so it's
basically comparing these two integers
with each other so let me just print
this system outprint ln yeah yeah
all right so you will see that
it returns minus one okay so this means
that i1 is less than i2 because we
called it like this i1 dot compared to
i2 so minus1 means that I1 is less than
I2. Let's say it would have been 30.
Now it's returning one. This means I1 is
greater than I2. Okay. And let's say
both are 20.
Now it will return zero. So zero means
that both are exactly the same. Okay. So
let me make it 10 again. Yeah.
Similarly, we have I1 dot uh we have a
sum method as well. Okay. And we have
max and min methods as well. Let me show
all these one by one. First let me call
integer domax. So I can pass these two
in it I1 and I2. And let me print this
turn.out.print
ln.
So it will just guess it will just tell
you which one is the maximum among these
two. Okay. So if I call this great. So
this returns minus one and this will
return 20 because 20 is the greater one
among these two. Okay. Similarly
we also have integer dot min and this
will tell you the minimum one. All right
which is 10. And we can also use integer
dot sum and this will just add these two
integer values and you will get the
result. All right. So we have these many
utility methods inside it and we also
have some fields inside it like I'll
show you we have integer dot
max value okay this is a static final
field in this class integer and this is
the maximum value of which is there in
the integer which is present in in any
integer. Okay, you can see that max
value is this. So you might have used
this a lot when you try to solve your
data structure problems. You you might
have seen this integer domax value and
similarly we have integer dot min value.
So these are basically
predefined and you can directly use
them. Okay. Min value is the least value
which is there. Okay. Minus 214. So let
me print these as well.
Okay. So this is the max value of an
integer and this is the minimum value of
an integer. All right. So these are some
important methods, some utility
functions and utility variables that are
there in this class and we often use
them daily. Okay. So that's why Java has
provided a wrapper class as well so that
it's easy for us to write code. Okay.
And there was one more reason at some
places you cannot even use int directly.
Okay. There they have not allowed you uh
you to use int. Okay. For example inside
a list. Let's say if you want to create
a list. So this list list is equal to
new array list. Okay.
So here you have to tell that the which
what is the type of this list. Okay. So
let's say you want to create a list of
int. Okay. List of an integer. So if
you'll try to give it like this, it
won't work. Okay. It will give an error
that type argument cannot be of a
primitive type. Okay. So here you will
have to use integer.
All right. like this.
See there is no error now. Okay. So that
is why at some places in Java there is
mandatory to use these wrapper classes.
Hence that is also one more reason for
having these classes. Okay. And let's
say if I add some values
place dot add to okay. So you can see
that I am directly adding int values
here. Okay. I'm not add I'm not creating
an integer object and adding it
specifically. I'm just adding int
values. So actually whenever you do list
dot add again Java does autoboxing.
Okay. It automatically does the boxing
and it automatically converts this int.
Okay. And let there is another example.
Let's say we have a set. So here also if
I'll try to have an int value like this.
Okay. new hash set
it won't work okay because again
primitive primitive data type does not
work here all right so here also I will
have to take it like integer and by the
way these are the generics like these
two arrows this and this these are
called generics so if you go to the list
interface here they have defined these
generics so in these generics you have
to pass what is the type of the list
okay whether it's a list of integer or a
list of double or a list of string. So
in these generics as I was saying you
cannot use primitive data types. Okay.
So in set also you will see that you
will have to use integer and if you'll
add something here again you can
directly add an int value. Compiler will
automatically do the boxing logic. Okay.
So I hope you understood what rapper
classes are and as you can see it's a
very simple topic but still it's asked a
lot in interviews and if you have not
heard about rapper classes or this
boxing autoboxing unboxing then you
might not be able to answer this
question. Okay so I hope you like the
video and if you want more videos on
Java core interview concepts then please
stay tuned and subscribe to my channel
the curious coder. Okay.
More transcripts
Explore other videos transcribed with YouTLDR.

In A World of Systems
Donella Meadows · Spanish

Matriks Matematika Wajib Kelas 11 Bagian 2 - Operasi Matriks
m4th-lab · English

Dialog: Korupsi Ancam Demokrasi Rakyat
KOMPASTV · English

Chapter [4] Filsafat Ilmu
Syamsul Official · English

(Lagu) CELENGAN SEL | Perbedaan Sel Hewan dan Sel Tumbuhan
BacaBaca · English

LEILÃO VIRTUAL EB CORTE MS - CAMPO GRANDE/MS - RURAL PLAY
Rural Play · English

Praktikum Biokimia I Tes Karbohidrat Part 1
Rahmawati · English

Podfalar (15-07-2026)
Rádio Mídia · English

¿Qué distingue a los humanos de los animales? | DW Documental
DW Documental · Spanish

34 Funciones trigonométricas II
Píldoras matemáticas · English

Expertos FORENSES examinan una ESCENA DEL CRIMEN...
Víctor González · English

Curso de HTML y CSS desde CERO (Completo)
Soy Dalto · Spanish
Get the TLDR of any YouTube video
Transcribe, summarize, and repurpose videos in 125+ languages — free, no signup required.