Java Program to Convert Primitive Types to Wrapper Objects

In the previous article we have discussed Java Program to Convert Boolean to String

In this article we will see how to convert primitive types  to objects and vice versa.

Program to Convert Primitive Types to Wrapper Objects

We have created objects of Wrapper class (Integer, Double, and Boolean) and change the objects into corresponding primitive types.

Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples

Approach:

  1. Declare the object of integer, double, boolean in variable ob1, ob2, ob3 respectively.
  2. Convert the objects to its primitive type the store it to a variable .
  3. Display the results

Program :

import java.util.*;

public class Main

{

    public static void main(String[] args)
    
    {
        //Scanner class object Created
        Scanner sc=new Scanner(System.in);
        //Enter inputs
        int integer_input=sc.nextInt();
        double double_input=sc.nextDouble();
        boolean boolean_input=sc.nextBoolean();
        
        // creating objects of wrapper class
        Integer ob1 = Integer.valueOf(integer_input);
        Double ob2 = Double.valueOf(double_input);
        Boolean ob3 = Boolean.valueOf(boolean_input);
        
        // converting into primitive types
        int output1 = ob1.intValue();
        double output2 = ob2.doubleValue();
        boolean output3 = ob3.booleanValue();
        
        // printing the primitive values
        System.out.println("The Converted value of int variable: " + output1);
        System.out.println("The Converted value of double variable: " + output2);
        System.out.println("The Converted value of boolean variable: " + output3);
        
    }
}
Output :

45
56.67
true
The Converted value of int variable: 45
The Converted value of double variable: 56.67
The Converted value of boolean variable: true

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Related Java Program: