Autoboxing and Unboxing in Java
Introduction
Autoboxing and unboxing are features in Java that automatically convert between primitive types and their corresponding wrapper classes. This feature simplifies coding by eliminating the need for explicit conversions.
Autoboxing
Autoboxing is the automatic conversion of a primitive type into its corresponding wrapper class.
Example:
int x1 = 10; // Primitive type
Integer x2 = x1; // Autoboxing: int to Integer
Unboxing
Unboxing is the automatic conversion of a wrapper class into its corresponding primitive type.
Example:
Integer x2 = new Integer(10); // Wrapper class object
int x1 = x2; // Unboxing: Integer to int
Example Code
Here's an example to illustrate autoboxing and unboxing:
public class AutoboxingUnboxingExample {
public static void main(String[] args) {
int x1 = 10; // Primitive type
Integer x2 = x1; // Autoboxing: int to Integer
int x3 = x2; // Unboxing: Integer to int
System.out.println(x1); // 10
System.out.println(x2); // 10
System.out.println(x3); // 10
}
}
In this code:
x1
is a primitiveint
.x2
is anInteger
object created by autoboxingx1
.x3
is a primitiveint
created by unboxingx2
.
Comparison of Wrapper Objects
When comparing wrapper objects, it's important to remember that they are objects and comparisons should consider object references.
Example:
public class WrapperComparison {
public static void main(String[] args) {
Integer x1 = new Integer(10);
Integer x2 = new Integer(10);
if (x1 == x2) {
System.out.println("Same");
} else {
System.out.println("Not Same"); // This will be printed
}
}
}
In this code, x1
and x2
are different objects with the same value. The ==
operator compares object references, so the output is "Not Same".
Special Case: Integer Caching
Java caches integer values from -128 to 127. When autoboxing, if the value is within this range, the same object reference is used.
Example:
public class IntegerCaching {
public static void main(String[] args) {
Integer x1 = 40;
Integer x2 = 40;
if (x1 == x2) {
System.out.println("Same"); // This will be printed
} else {
System.out.println("Not Same");
}
Integer x3 = 200;
Integer x4 = 200;
if (x3 == x4) {
System.out.println("Same");
} else {
System.out.println("Not Same"); // This will be printed
}
}
}
In this code:
x1
andx2
are the same object because 40 is within the cached range.x3
andx4
are different objects because 200 is outside the cached range.
Key Points
Autoboxing: Automatic conversion of primitives to their corresponding wrapper classes.
Unboxing: Automatic conversion of wrapper classes to their corresponding primitive types.
Object Comparison: Use
.equals()
for value comparison and==
for reference comparison.Integer Caching: Java caches integer values from -128 to 127.
Interview Questions
What is autoboxing and unboxing in Java?
- Autoboxing is the automatic conversion of a primitive type to its corresponding wrapper class. Unboxing is the reverse process.
Why are autoboxing and unboxing useful in Java?
- They simplify code by eliminating the need for explicit conversions between primitive types and their wrapper classes.
What happens when you compare two wrapper objects using
==
?- The
==
operator compares object references, not values. Use.equals()
to compare values.
- The
Explain the concept of Integer caching in Java.
- Java caches integer values from -128 to 127. When autoboxing, if the value is within this range, the same object reference is used.
What will be the output of the following code?
Integer x1 = 1000; Integer x2 = 1000; if (x1 == x2) { System.out.println("Same"); } else { System.out.println("Not Same"); }
- The output will be "Not Same" because 1000 is outside the cached range, so
x1
andx2
are different objects.
- The output will be "Not Same" because 1000 is outside the cached range, so