Related Topics

JAVA Programming
int i = 10;
Integer j = new Integer(i); // manually boxing the int i into an Integer object
However, with autoboxing, we can write the same code more simply, like this:
int i = 10;
Integer j = i; // using autoboxing to convert the int i to an Integer object
In this example, the value of the int
variable i
is automatically converted to an Integer
object and assigned to the Integer
variable j
. The conversion is done automatically by the Java compiler, without the need for explicit boxing or unboxing.
Similarly, we can use autoboxing to convert an Integer
object to an int
primitive type, like this:
Integer j = 10;
int i = j; // using autoboxing to convert the Integer object j to an int value
In this example, the value of the Integer
object j
is automatically converted to an int
primitive value and assigned to the int
variable i
. Again, this is done automatically by the Java compiler, without the need for explicit unboxing.
Autoboxing makes code more concise and easier to read, and helps to reduce the potential for errors that can occur with manual boxing and unboxing.
int i = 10;
Integer j = i; // autoboxing
int k = j; // unboxing
In this code, the int
value 10
is automatically boxed into an Integer
object and assigned to the variable j
using autoboxing. Later, the value is unboxed and assigned to the int
variable k
. The autoboxing and unboxing happen implicitly and automatically by the Java compiler.
int i = 10; // primitive type
Integer integer = i; // autoboxing, i is automatically converted to Integer
int j = integer; // unboxing, integer is automatically converted to int
double d = integer; // casting, integer is explicitly cast to double
In this example, we first declare a primitive int
variable i
and assign it the value 10. We then declare an Integer
variable integer
and assign it the value of i
. This is an example of autoboxing, where the primitive int
is automatically converted to its corresponding wrapper class Integer
.
Next, we assign the value of integer
to a primitive int
variable j
. This is an example of unboxing, where the wrapper class Integer
is automatically converted back to its primitive type int
.
Finally, we declare a double
variable d
and assign it the value of integer
. This is an example of casting, where the wrapper class Integer
is explicitly cast to the primitive type double
.
int a = 10;
Integer b = a; // autoboxing int to Integer
int c = b; // autounboxing Integer to int
In this example, the int
variable a
is automatically converted to an Integer
object when assigned to the Integer
variable b
. Similarly, the Integer
object b
is automatically converted to an int
primitive when assigned to the int
variable c
.
The Integer
, Long
, Float
, and Double
classes also provide a range of useful methods for working with their corresponding primitive types. For example, the Integer
class provides methods for converting between int
and String
, as well as for performing arithmetic and bitwise operations on int
values. The Long
, Float
, and Double
classes provide similar methods for working with their respective primitive types.
// Autoboxing example with Integer wrapper class
int num = 10; // primitive data type
Integer numWrapper = num; // autoboxing from primitive to object
System.out.println("num: " + num);
System.out.println("numWrapper: " + numWrapper);
// Unboxing example with Integer wrapper class
int numUnboxed = numWrapper; // unboxing from object to primitive
System.out.println("numUnboxed: " + numUnboxed);
In this example, num
is a primitive int
data type, and numWrapper
is an Integer
object. When num
is assigned to numWrapper
, the Java compiler automatically converts the primitive int
to an Integer
object, which is known as autoboxing.
The output of the program will be:
num: 10
numWrapper: 10
numUnboxed: 10
In the second part of the example, numWrapper
is assigned to numUnboxed
, which unboxes the Integer
object to a primitive int
. The value of numUnboxed
is then printed to the console, which will output the same value as num
since they are both primitive int
data types.
Integer numObj = 10;
int num = numObj.intValue(); // unboxing
System.out.println(num); // output: 10
In this example, we create an Integer
object numObj
with a value of 10. We then unbox the Integer
object to an int
primitive using the intValue()
method and assign it to the variable num
. Finally, we print the value of num
to the console. The output is 10
, which is the value of the Integer
object after unboxing.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.