Object Oriented Programming: Unit III: Exception Handling and Multithreading

Autoboxing

Definition, Example Java Programs

An automatic conversion of primitive data type into equivalent wrapper type is called as autoboxing.

Autoboxing

Definition: An automatic conversion of primitive data type into equivalent wrapper type is called as autoboxing.

This is a new feature of Java5

Example Program

class AutoboxExample

{

public static void main(String args[])

{

//auto-boxing, an int is boxed into Integer object

Integer obj = 111;

//unboxing

int val = obj.byteValue();

System.out.println("Object value = "+obj);

System.out.println("The primitive value = "+val);

}

}

Output

Program Explanation

In above program,

(1) We are autoboxing an integer type value.

(2) Then using the byteValue() method we are unboxing this value. This method converts the given number into a primitive byte type and returns the value of integer object as byte.

(3) Finally we are displaying both the object value and primitive value.

Object Oriented Programming: Unit III: Exception Handling and Multithreading : Tag: : Definition, Example Java Programs - Autoboxing