Every primitive type has a wrapper class.
Java is object oriented, that means there is a class for every primitive type.
In this lesson, you'll learn about wrapper classes.
You can find the code for this lesson by following this path in the resources.
Let's get one question out of the way, what is a rapper?
A rapper is an immutable class that wraps around a primitive type.
Here's an example, integer integer is an immutable class that wraps around the type because integer
is an immutable class, then variables of that type story, a reference to an object.
They can be now, they can call methods, but they cannot change, which means they are immune to the
reference trap.
So inside Rupert Java, there are a bunch of primitive variables if you view the runtime.
You'll notice that a primitive is just a value and nothing more, and because it's primitive, the variable
cannot be null and it cannot call any methods.
And so we can set up a variable of type integer.
Equal to a new object of the integer class and then pass the integer value into the constructor.
And now the variable stores a reference to an integer object, the integer object contains a field that
matches the value we pastan.
And because it's an object, the variable can call many methods like.
Percent to string int value, double value and so much more.
And because it's an object, it can also be No.
But there's a shorter way to define a proper object.
The syntax where you directly create a new object of the integer class with the value inside the constructor
is deprecated.
You can just assign the value directly in Java is going to create an object and update the field behind
the scenes.
You can confirm this by running the debugger.
And like before, the variable stores, a reference to an integer object and the integer object contains
a field that matches the value of Pastan.
And here are some more upper classes, double long and character like integer, they are immutable class
types, variables of each type story reference to an object they can be know they can call methods they
cannot change.
You cannot update the status of an immutable object, which means they're immune to the reference trap.
So inside, Rupert Jarvis said the three variables equal to a new object of the respective wrapper class,
first, we're going to do with the Longway.
All the rerun, the debugger.
Step through each line.
And notice that every object contains a field that matches the value that we bastin because they're
all objects, each one can be null and can call methods.
And so in general, the syntax where you create a new object of the wrapper class is deprecated, you
can just assign the value directly and Jeff is going to create an object and update every field behind
the scenes.
Let's talk about the reference trap rappers are immutable class types, so they are immune to the trap.
A rapper such as Integer doesn't have any suitors, so it's impossible to update the field.
Let's set another integer variable equal to the first.
Launched the debugger, and it follows that when you set an integer variable equal to another, it copies
of the reference inside and now both variables are going to point to the same integer object.
Should I worry about the reference trap?
No, it's impossible to update the state of the object when you assign the variable annuity Energia.
When you assign the variable a new integer, it's going to equal a brand new object of the integer class
with the value 10, the same applies to the other rappers.
OK, so you might be asking one to use primitive one to use proper use, primitive 90 percent of the
time, primitive takes up less space and they're faster, only use a wrapper when you need the variable
to be null or when you need to call a method from the variable.
In this lesson, you learned about wrapper classes.
A wrapper class wraps around a primitive type javas object oriented, so there's a class for every primitive
type.
Use a primitive type 90 percent of the time, because they take up less space and they're faster, only
use a wrapper when you absolutely need to, when you need the variable to be known or if you need to
call a method from the variable.