image
Autoboxing & Unboxing
Start course
Difficulty
Beginner
Duration
10m
Students
98
Ratings
4.7/5
Description

In this course, we will learn about wrapper classes, auto-boxing, and unboxing concepts.

Learning Objectives

  • What is Wrapper Class?
  • Conversion of Types
  • Autoboxing - Unboxing

Intended Audience

  • Anyone looking to get Oracle Java Certification
  • Those who want to learn the Java Programming language from scratch
  • Java developers who want to increase their knowledge
  • Beginners with no previous coding experience in Java programming
  • Those who want to learn tips and tricks in Oracle Certified Associate – Java SE 8 Programmer certification exams

Prerequisites

  • No prior knowledge is required about the Java programming language
  • Basic computer knowledge
Transcript

Hi there. In the previous video, we talked about the type conversion between the primitive and wrapper classes. So we can easily convert the primitive and wrapper to each other with Java 5 and later. So this concept is named autoboxing and unboxing in java. So, what are the autoboxing and unboxing?

Let's dive in. Before autoboxing and boxing, type conversion was done manually as you see in the previous slide. But since Java 5, the whole process happens automatically without writing any code for conversion. Java provides two features called autoboxing and auto unboxing. Okay, let's look at what they are? When java automatically converts a primitive type, like int, into a corresponding wrapper class object, example, integer, it is called autoboxing. When java automatically converts a wrapper class, like integer, into a corresponding primitive type, example, int, then it is called auto unboxing. Primitive to wrapper is autoboxing. Wrapper to primitive is unboxing. Let's make some examples with autoboxing and unboxing. In exercise project, right click on the source folder and select new class. I'll specify the package name as boxing and the class name can be AutoboxingUnboxing, and select the checkbox for the main method. And click the finish button to create this class. 

Okay, let's declare a variable named myDouble with wrapper class double, and assign the value 12.8 to it. This is an autoboxing of double. The 12.8 is primitive and the myDouble is the object of the double wrapper class. So we the primitive to the wrapper and this process is named autoboxing because automatic conversion of primitive double data type into wrapper double data type is done automatically. Let's declare a variable named otherDouble with primitive double type and assign variable myDouble to it. This is auto unboxing of double because the automatic conversion of primitive data type into corresponding wrapper data type is done automatically.

 I'll print the value of the variable myDouble and otherDouble by using the print method, so we will observe its value. Okay, in another example, let's declare a variable named myInteger with wrapper class integer  and assign value 5 to it. This autoboxing of int, the five is primitive and the myInteger is the object of the integer wrapper class. So we assign the primitive to the wrapper. And this process is named autoboxing because automatic conversion of primitive int data type into wrapper integer data type is done automatically. Okay, now let's declare a variable named myInt with primitive int type and assign variable myInteger to it. This is auto unboxing of integer. And I'll print the value of variables myInteger and myInt by using the print method. 

So we will observe its value too. In the last example, let's declare a variable named myCharacter with wrapper class character and assign the value letter 'b' between single quotes. This is autoboxing of char. Let's declare a variable named myChar with primitive char type and assign the variable myCharacter to it. This is auto unboxing of character. We print the value of variables, myCharacter and myChar, by using the print method. Okay, let's run the code. You see the results in the console. We have successfully performed the conversion between primitive and wrapper classes. Now we'll talk about an important issue and end the lesson. Notice that integer or double is a class, a wrapper class. Therefore, we can create objects from these classes and it's possible to assign a null value to these objects. 

For example, let's create an object of the integer wrapper class and assign it a null value, integer myInteger2= null. As you can see, we are not getting any errors, but we cannot assign null to primitive types. For example, let's create a variable of type primitive int and try to assign a null value to it; Int myInt2= null. As you can see, we are getting a compilation error. The error says, 'type mismatch, cannot convert from null to int'. So, if we perform the cast, will the error go away? If you pay attention, we achieve the compiler error, but this time we will get a null pointer exception error that will cause the runtime error because we can never assign a null value to primitive variables. Even if we write it like this, the result does not change, Int myInt2= myInteger2. If you notice, the compiler does not throw an error, but warns us that this will cause a null pointer exception. Yes, friends. This was the most important point you should pay attention to, converting between the primitive and wrapper classes. Let's take a short break here. See you in the next lesson.

 

About the Author
Students
4005
Courses
64
Learning Paths
5

OAK Academy is made up of tech experts who have been in the sector for years and years and are deeply rooted in the tech world. They specialize in critical areas like cybersecurity, coding, IT, game development, app monetization, and mobile development.

Covered Topics