How to convert Wrapper Objects to Primitive Types in Java?


In Java, A primitive type (i.e., int, double, float, etc.) is a basic data type that is built into the language and is not an object. Primitive types have a fixed size and can be used directly in calculations and operations.

A wrapper object is nothing but an object representation of a primitive type. For example, the Integer class is a wrapper object for the int primitive type. Wrapper objects are instances of classes and have methods that can be called, while primitive types do not.

However, using wrapper objects requires more memory than using primitive types, as each wrapper object must be created and stored in memory. In conclusion, while primitive types are simpler and more efficient, wrapper objects provide an object-oriented interface to primitive data and offer more functionality.

As per the problem statement we have to convert the given wrapper objects to respective primitive types.

Let’s start!

To show you some instances

Instance-1

  • Integer integer = new Integer(10);

  • int primitiveInt = 10;

Instance-2

  • Double doubleWrapper = new Double(10.5);

  • double primitiveDouble = 10.5;

Instance-3

  • Long longWrapper = new Long(10L);

  • long primitiveLong = 10L;

Algorithm

Algorithm-1: (By Using intValue() Method)

  • Step-1 − Creates an Integer wrapper object and assigns it a value of 10.

  • Step-2 − Converts the Integer object to a primitive int using the intValue() method.

  • Step-3 − Prints its value.

Algorithm-2: (By Using valueOf() Method)

  • Step-1 − Creates a Double wrapper object and assigns it a value of 10.5.

  • Step-2 − Converts the Double object to a primitive double using the valueOf() method.

  • Step-3 − Prints its value.

Algorithm-3: (By Using the Unboxing Operator (int))

  • Step-1 − Creates a Long wrapper object and assigns it a value of 10L.

  • Step-2 − Converts the Long object to a primitive long using unboxing.

  • Step-3 − Prints its value.

Algorithm-4: (By Using Type Casting)

  • Step-1 − Creates a Float wrapper object and assigns it a value of 10.5f.

  • Step-2 − Converts the Float object to a primitive float using type casting.

  • Step-3 − Prints its value.

Multiple Approaches

We have provided the solution in different approaches.

  • Using the intValue() method

  • Using the valueOf() method

  • Using the unboxing operator (int)

  • Using type casting

Let’s see the program along with its output one by one.

Approach-1: By Using intValue() Method

In this approach, we declare a random integer value and create an integer wrapper object. Then by using intValue() method we convert it into primitive type.

Example

public class Main {
   public static void main(String args[] ) {
      
      // Create an Integer wrapper object
      Integer integer = new Integer(10);
  
      // Convert the Integer wrapper object to a primitive int using intValue()
      int primitiveInt = integer.intValue();

      // Print the primitive int value
      System.out.println("Primitive int value: " + primitiveInt);
   }
}

Output

Primitive int value: 10

Approach-2: By Using valueOf Method

In this approach, we declare a random double value and create a double wrapper object. Then by using valueOf() method we convert it into primitive type.

Example

public class Main {
   public static void main(String args[] ) {
      
      // Create a Double wrapper object
      Double doubleWrapper = new Double(10.5);
 
      // Convert the Double wrapper object to a primitive double using valueOf()
      double primitiveDouble = Double.valueOf(doubleWrapper);

      // Print the primitive double value
      System.out.println("Primitive double value: " + primitiveDouble);
   }
}

Output

A Primitive double value: 10.5 

Approach-3: By Using the Unboxing Operator

In this approach, we declare a random long value and create a long wrapper object. Then by using unboxing operator method we convert it into primitive type.

Example

public class Main {
   public static void main(String args[] ) {
      
      // Create a Long wrapper object
      Long longWrapper = new Long(10L);
      
      // Convert the Long wrapper object to a primitive long using unboxing
      long primitiveLong = longWrapper;
      
      // Print the primitive long value
      System.out.println("Primitive long value: " + primitiveLong);
   }
}

Output

Primitive long value: 10

Approach-4: Using Type Casting

In this approach, we declare a random float value and create a float wrapper object. Then by using type casting method we convert it into primitive type.

Example

public class Main {
   public static void main(String args[] ) {
      
      // Create a Float wrapper object
      Float floatWrapper = new Float(10.5f);
      
      // Convert the Float wrapper object to a primitive float using type casting
      float primitiveFloat = (float) floatWrapper;     
      
      // Print the primitive float value
      System.out.println("Primitive float value: " + primitiveFloat);
   }
} 

Output

Primitive float value: 10.5

In this article, we explored different approaches to convert wrapper objects to primitive types by using Java programming language.

Updated on: 04-May-2023

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements