Articles

Java Basics – Boxing – Unboxing – Wrapper Classes

September 23, 2019


>>Hi, my name is Margret. In this video I’m
going to introduce you to autoboxing and unboxing. Autoboxing is the
automatic conversion that the Java compiler makes
between a primitive type and its corresponding
object wrapper class. Let’s look at an example. Let’s say we have an integer
n1 and we assign the value 7. n1 is a type int, which is a
value type or primitive type in Java of value types. And the value is stored
directly in the variable. Now, I’m going to add
a second statement and declaring a variable n2 of type uppercase
Integer and assigning n1. Notice, n2 is a reference type
variable, so here is no value, but a reference pointing
to the object in the heap. And the object on the
heap includes the value 7. This process of converting
a value type to a reference type
is called boxing. It is performed automatically
by the Java compiler. The compiler translates n1
to integer dot value of n1. The Java compiler caused the
method value of static method of type Integer,
passes n1 as an argument and thus receives the boxed
object as a return value. Now, I want to talk a bit
about when autoboxing occurs. The Java compiler applies
autoboxing in 2 situations. A; when the primitive value
is passed as a parameter to a method that expects the
corresponding wrapper class. B; when the primitive value
is assigned to a variable of the corresponding
wrapper class. Once again, let’s
look at some examples. First, an example of the
value passed as a parameter. Here I have a variable
called numbers. It is declared as
a List of Integers, notice upper case Integer. They initialize it
with a new Array List, which is still empty
at this point. And now we want to add some
values, so I say numbers add 13, 13 of course, is an int
literal and my list is a list of uppercase Integers. Not a problem because the
Java compiler is going to perform the conversion
for us. It’s going to turn the int
13, the uppercase Integer 13. So, here is a second example. We have a variable
number of type Integer. And if we want to assign
a value, let’s say 13. Once again, 13 is an int
literal and my number is of type uppercase Integer. The java compiler performs
the conversion for us. It does the autoboxing that
changes 13 of type int to 13 of the reference type
uppercase Integer. Unboxing is the reverse
process of boxing. It converts an instance
of the wrapper class to its corresponding
primitive type. Let’s look at an example. We have a variable num1
of type uppercase Integer, that’s the reference type. So, here you can see num1 and
the variable has a reference to an object in the heap. In my object in the heap,
I have the number 17. I have the number 17 because
I was assigning 17 and we know that already from before
that int literal 17, was automatically converted in uppercase Integer
object that contains 17. I’m going to add a
second statement here. I’m creating a second
variable num2. This 1 is of type
int primitive type. And I’m assigning num1. So, notice, num2,
is a primitive type, so my value is stored
directly in the variable. I store the value 17 because
this was the value of num1. And num1 had a object
with the value 17, which was unboxed
to a value type 17. Notice, the compiler caused
intValue on the variable num1, num1 of course, is
my reference type, intValue converts the
object that includes 17, to a primitive type int. And here I have my primitive
type int 17 that can be assigned to my primitive type int num2. One more example. Here I have a method, it’s
called sumEven because it’s sums up all the even numbers. And I’m passing a
list of integers. I call my list numbers and so
I’m declaring a variable sum, initialize it with zero. For all my Integers n, in my
list numbers, I make this fall in check, if check
with an n is even. It’s even when n
module is to zero. And if that is the case,
then I add it to my sum at the end I return my sum. Notice, that modules and the
compound operator, plus, equals, are defined for primitive
types like int, but not defined for
reference types. However, n is of type
uppercase Integer. The java compiler
caused intValue on n and intValue rates the number
of my uppercase Integer object n and returns the primitive
type intValue. You can see this
conversion happens twice, here with the modulus operator, here again with the
compound assignment operator. At this point, I want to
look at the primitive type versus the corresponding
wrapper class. Primitive types are
always value types. The corresponding
wrapper classes are always reference types. So, this is the most
important difference. Primitive types have a given
range, true, false for Boolean or certain number
range for integers. The corresponding wrapper
class has the exact same range, plus null. Once again, an example. Primitive type, stored
directly in the variable, corresponding wrapper class,
wrapped as an object under heap. At this point I want
to have a closer look at the wrapper classes. So far we only looked at
int and uppercase Integer, the corresponding
wrapper class to int. Each of the 8 primitive
types has a corresponding wrapper class. Notice they are all spelled with the first uppercase
letter, like reference types. Also notice, there is an
extra abstract class Number. By the way, I can
tell in my UML diagram that it’s an abstract class because it is printed
in italics. Number, is the super class for
the 6 Number classes, Byte, Short, Integer, Long,
Float and Double. All of the wrapper
classes have the same range as the corresponding
primitive type class null. All wrapper classes are final,
which means we can use them, but we cannot derive from them. We cannot use them
as a super class. Now, I want to briefly discuss
why we use wrapper classes. There are 2 main reasons. One is the useful functionality that is provided
the wrapper classes. The other 1 is the fact that generics can only
accept reference types. And since we can’t use
generics with primitive types, it’s very helpful to have
a corresponding class that can be used. I’m not going to discuss
generics here, this is going to happen in a different video, but I want to show
you a few examples of the useful functionality that
is provided in wrapper classes. Integer, for example, has
a method called parseInt and it allows me to
parse an integer. I could say integer
number is Integer parseInt and then passing 1 2 3 and
now I print out my number, 1 2 3 and the corresponding
number value. Let’s compile and run. As you can see, the string 12 3, is converted to the
number 1 2 3. So, to make this extra clear, I could also add some
quotes before and after. So, we can see the first
1 is actually a string. Let’s compile and run again. That makes it nice and clear. It also has constants. For example, a constant
called ma value. This time I’m just copy,
pasting the quote in here. You can see we have
the max integer. I am providing my
value in 2 new lines and here is my constant
max value. It’s a static constant
that I call on the type. I compile and run and you
can see a little bit more than 2 billion is my max value. If this is hard to read, you could have a
comma separator here. It makes it a little easier, so now everybody can
see my 2 billion, 147 million, 483 thousand, 647. Now, I want to show
you some examples with the wrapper
class character. I could have character,
I call it ch and I assign it x. The class
uppercase Character has a whole number of methods that allow us to check whether my Character
is a specific instance of something. For example, is it a letter? Is it a number? Is it an uppercase, excreta? So, here we’re checking
isletter, isDigit, that’s the number of
course, isUpperCase. And I’m going to
compile and run. And it tells me x
is a letter, yes. It’s not a number,
it’s not an uppercase. Now that you’ve seen the
advantages of wrapper class, I also want to point out
when not to use them. One thing to keep in mind is that wrapper classes are less
efficient than primitive types. So, as a best practice,
keep in mind, when a primitive type can do the
job as well as a wrapper class, prefer the primitive type.

No Comments

Leave a Reply