Unboxing is a process of converting an object of the wrapper class in JAVA into a primitive data type. It is present as part of the “java.lang” package and can be used in jAVA programs after this package is imported. For example, converting the wrapper class Integer’s object into Int. It is the reverse of autoboxing in JAVA. It was introduced as part of development in JAVA 5, making the developer’s life much easier.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
Syntax
The syntax used for unboxing in JAVA is provided below:
import java.lang.* class mainClass { public static void main (String args[] ) { Integer variableName=new Integer( VariableValue); variableName2 = variableName; //Unboxing in JAVA } }
Here, The object of wrapper class “Integer” is assigned to primitive data type Int and the value is passed as a parameter in the wrapper class’s constructor. This happens implicitly in autoboxing.
Unboxing is the process of converting an object into a primitive data type. This process is done by the JAVA compiler automatically as the libraries in JAVA support it in JAVA 5th edition and onwards. There are two prerequisites that need to be there in place for unboxing to run in by the JAVA compiler. These two prerequisites are enlisted below:
Below is the table containing wrapper class and its related primitive data type :
Wrapper class | Related primitive data-type |
Boolean | boolean |
Integer | Int |
Float | float |
Character | char |
Byte | byte |
Long | long |
Short | short |
Double | double |
The data flow and explanation of unboxing in JAVA is explained properly with examples in the below section.
Some examples are provided below will provide a conceptual clarity of the unboxing technique.
Code:
public class test1 { public static void main(String args[]) { Integer var1=new Integer(50); if( var1 > 10) // Unboxing using comparator. { int var2=var1 + 10; int var3=var2; System.out.println(" The value of variable using unboxing functionality is JAVA is :"+ var3); } else { int var2=var1 - 10; //Unboxing using assignment operator. int var3=var2; System.out.println(" The value of variable using unboxing functionality is JAVA is :"+ var3); } } }
Output:
Explanation: In this program, The main class is declared as “test1” as it contains the main() method. The program execution starts with the main() function. An object of the wrapper class “Integer” is created with the name “var1” and assigned with the value “50”. You should focus on the syntax of assigning value to variable “var1”, which is different in comparison to autoboxing. Here, the object is used instead of the data type for declaration and assignment purposes. Once the assignment is done, unboxing is done for that object.
Here, a comparison operator is used to unboxing the object. “If” logic checks if the value of “var1” is more than 10 or not. If not, then the control flows to another part of the program, starting with the “else” keyword and the whole code snippet under if loop will be skipped. In the else section, there is no comparator operator, so it enters the control logic. Assignment operator “=” does the unboxing part in case else is invoked. You can change the value of “var1” by changing the parameter provided to the wrapper class’s constructor (“Integer()” in this example). Finally, value is added or subtracted and printed as per logic.
Code:
public class test2 { public static void main(String args[]) { Character charName = 'M'; // Autoboxing. char charName2 = charName; // Unboxing System.out.println("The process used here is auto-unboxing to display the character : "+ charName2 ); } }
Output:
Explanation: Here, unboxing is done using the assignment operator. The data flow and control execution will work, as explained in the previous example. Here one thing to notice is that we have not used the object of a wrapper class to declare and assign the value to the variable “charName”. Although unboxing is done on “charName” using the assignment operator.
Code:
public class test3 { public static void main (String args[]){ Integer varName = new Integer("1000"); int varName2 = varName.intValue(); System.out.println("Variable name is printed after unboxing using a built-in function is : " + varName2); } }
Output:
Explanation: This works similar to the previous example with an added function called “intValue(). This function should extract the value from the variable “varName” and assign it to another variable named “varName2”. The function “intValue()” explicitly returns the value of object ”varName”. This is exactly what the compiler does in the backend. You should try removing this function and see the results to compare it with example number 2.
Some of the primitive advantages of unboxing in JAVA is presented in the form of the list below:
Unboxing is the reverse of autoboxing in JAVA. It is converting the wrapper class’s object into a primitive data type. Although we have the functionality of declaring and assigning variables using primitive data type directly (That is called the autoboxing feature of JAVA), objectification of wrapper class and then assigning this object to a primitive data type is used several times when standardization of code is important. This reveals the true object-oriented property of JAVA. It is used mostly in big projects for easy maintenance.
The above is the detailed content of Unboxing in Java. For more information, please follow other related articles on the PHP Chinese website!