Java Program to convert primitive types to objects and vice versa

To understand this example, you should have the knowledge of the following Java programming topics:


Example 1: Java Program to Convert Primitive Types to Wrapper Objects

Output

An object of Integer is created.
An object of Double is created.
An object of Boolean is created.

In the above example, we have created variables of primitive types (int, double, and boolean). Here, we have used the valueOf() method of the Wrapper class (Integer, Double, and Boolean) to convert the primitive types to the objects.

To learn about wrapper classes in Java, visit Java Wrapper Class.


Example 2: Java Program to Convert Wrapper Objects to Primitive Types

Output

The value of int variable: 23
The value of double variable: 5.55
The value of boolean variable: true

In the above example, we have created objects of Wrapper class (Integer, Double, and Boolean).

We then change the objects into corresponding primitive types (int, double, and boolean) using the intValue(), doubleValue(), and booleanValue() methods respectively.

Note: The Java compiler automatically converts the primitive types into corresponding objects and vice versa. This process is known as autoboxing and unboxing. To learn more, visit Java autoboxing and unboxing.