hi in this lesson we'll look at wrapper classes we know that there are differences between primitive and reference types in java each of the primitive types int double etc are built into the language while objects and classes are created by the programmer one problem that comes from having both primitive and reference types is the fact that some objects will only interact with other object data types arraylists for example are a common data storage type that will only store object data types and not primitive values but what if we wanted to store those primitive values in an arraylist luckily we can actually convert primitive types to object types using something called a wrapper class for every primitive type in java there is a built-in object type called a wrapper class the wrapper class is a class that can contain primitive data types as object data types each java primitive has a corresponding wrapper class as noted in the table notice how the primitive types are lower case while the wrapper classes are all upper we will only study the integer and double wrapper classes but it's good to be aware of the others wrapper classes provide a way to wrap primitive values in an object so that primitives can do activities reserved for objects such as being added to an arraylist collection we will get into other data types later in this course wrapper classes provide an assortment of built-in utility methods for primitives like converting primitive types to and from string objects or comparing various objects sometimes you may need to create a wrapped object for a primitive type so that you can give it to a method that is expecting an object and this is where wrapper classes can help we can instantiate wrapper classes the same way that we do with other objects creating an integer object requires us to use the new operator followed by an int value just as with other object types we can only input acceptable data types for wrapper classes the data type must match the primitive data types integers can only be given int parameters and doubles must be given double values we can distinguish the primitive types from the wrapper classes by the lettering if the data type starts with an uppercase letter then it is a wrapper class otherwise the data type will be the primitive version just as other classes have methods the wrapper classes have methods that can be utilized let's discuss the integer. min and integer. max values we looked at these values in lesson 1.
5 note the integer class designation at the beginning these can be used to assign variables to the max and min possible values and are useful for upper and lower bounds of arrays which we will discuss later in the course the next method we'll look at is the int value method we will look at the double value method as well because they both serve the same purpose for the two different wrappers the int value method returns the numeric value represented by this object after conversion to type int the method does not accept any parameters the method returns the numeric value which is represented by the object after conversion to the integer type int value converts the object integer 68 to the primitive type value which is then assigned to int i i can then be used as a primitive value and is printed in the subsequent line of code a similar method exists for the double class where the double value is converted to the primitive type and then can be used as such having to convert between object and primitive values is a lot of work luckily java has a way of recognizing when a wrapper object needs to be converted to a primitive and vice versa and automatically makes that conversion happen this automatic conversion process is called autoboxing and unboxing autoboxing includes converting a primitive int to an object integer and a primitive double to an object double autoboxing is done automatically by the java compiler instead of having to write the full constructor java will convert the primitive value to the wrapper class if the data type declared at the beginning of the variable corresponds to the correct wrapper class the java compiler will also apply auto boxing when a primitive value is passed as a parameter to a method that expects an object of the corresponding wrapper class we will learn in greater detail about arraylists but for now we just need to know that when instantiated with integer arraylists are only supposed to accept integer value types as acceptable data types when we initially print the arraylist the list is empty when we try adding the primitive variable we can see that the value of primitive was successfully added to the list this is because java automatically converted the value of primitive to the integer class so that it could be added to the arraylist unboxing is the reverse of autoboxing unboxing is the automatic conversion that the java compiler makes from the wrapper class to the primitive type this includes converting an object integer to a primitive int and an object double to a primitive double instead of having to use the int value method as we did in a previous example we can actually assign an integer object to a primitive variable and it will automatically convert it to the correct type this example shows unboxing when an integer object is assigned back to a primitive int type the java compiler takes care of the unboxing conversion at runtime the java compiler will also apply unboxing when a wrapper value is passed as a parameter to a method that expects a primitive value in this example the rectangle constructor is only supposed to accept int values as actual parameters the wrapper class integer is unboxed when input to the rectangle constructor into a primitive value type which is then stored as the width value in the rectangle autoboxing and unboxing allow us to use primitive types and wrapper class objects interchangeably without having to perform any casting explicitly thus making it easier for programmers to write cleaner code that's easier to read just like the string class the integer and double classes are part of the java. lang package because these two classes belong to the java.