Java Primitive Array To Wrapper Array

Java Primitive Array To Wrapper Array | The wrapper classes in Java are used to convert primitive datatype to object and object into primitive datatype. The conversion of primitive datatype to an object is called autoboxing and converting an object to primitive datatype is called unboxing. The feature autoboxing and unboxing in java is used from J2SE 5.0.

Uses Of Wrapper Class
As in Java we mainly deal with the objects like collections, serialization, synchronization and many more so there is a need to use wrapper classes.

1) To change the value in the method:- As Java supports on call by value, if we pass the primitive value then it does not change the original value. So there is a need to convert the primitive value into the object. Hence the original value will be changed.
2) Serialization:-There is a need to convert objects into streams to perform serialization, so we can use wrapper classes to convert the primitive value to objects.
3) java.util.package:- this package provides the utility to deal with objects.
4) Collections framework:- All classes in the collection framework deal with the objects, but not with the primitive data types. If we have primitive values, then to work with the collections framework we have to convert them into wrapper classes.

Example of Java Primitive Array To Wrapper Array:-
We have an array of int.
int[ ] arr = {10, 20, 30};
We want to convert to its wrapper array,
Integer[ ] arr = {10, 20, 30};

Program for Java Primitive Array To Wrapper Array using Streams

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      double[] primitiveDoubles = { 1.2, 3.22, 10.33, 4.22, 5.33 };
      Double[] wrappedDoubles = Arrays.stream(primitiveDoubles)
                                      .boxed()
                                      .toArray(Double[]::new);

      System.out.println(primitiveDoubles.getClass().getName());
      System.out.println(wrappedDoubles.getClass().getName());
      System.out.println("Array = " + 
                          Arrays.toString(wrappedDoubles));
   }
}

Output:-

[ D
[ Ljava.lang.Double;
Array = [1.2, 3.22, 10.33, 4.22, 5.33]

In this output “[” represents an array, and “D” represents for double data type variable.

Let us see an another example to convert int array to Integer array by using Java 8 streams.

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      int[] primitiveInt = { 1, 2, 3, 1, 4, 5 };
      Integer[] wrappedInteger =  Arrays.stream(primitiveInt)
                                        .boxed()
                                        .toArray(Integer[]::new);

      System.out.println(primitiveInt.getClass().getName());
      System.out.println(wrappedInteger.getClass().getName());
      System.out.println("Array = " + 
                          Arrays.toString(wrappedInteger));
   }
}

Output:-

[ I
[ Ljava.lang.Integer;
Array = [1, 2, 3, 1, 4, 5]

Using Java 8 streams we can also convert wrapper class array to primitive arrays. Let us see it through an example program.

Java program to convert wrapper array to primitive array

import java.util.Arrays;

public class Main {
   public static void main(String args[]) {
      int[] primitiveInts = { 1, 2, 3, 1, 4, 5 };
      Integer[] wrappedInteger = Arrays.stream(primitiveInts)
                                       .boxed()
                                       .toArray(Integer[]::new);
      int[] unwrappedInts = Arrays.stream(wrappedInteger)
                                  .mapToInt(Integer::intValue)
                                  .toArray();

      System.out.println(primitiveInts.getClass().getName());
      System.out.println(wrappedInteger.getClass().getName());
      System.out.println(unwrappedInts.getClass().getName());
   }
}

Output:-

[ I
[ Ljava.lang.Integer;
[ I

In the very similar way, we can convert wrapper double to primitive double. For this we have to call mapToDouble() method.

double[] unwrappedDoubles = 
               Arrays.stream(wrappedDoubles)
                     .mapToDouble(Double::doubleValue)
                     .toArray();

In the previous programs we have used Java 8 Streams to convert Java primitive array to wrapper array. But it doesn’t work on byte array. For the byte array we have to define a separate method.

Program for Java Primitive Array To Wrapper Array

import java.util.Arrays;

public class Main {
   public static Integer[] toObject(int[] array) {
      if (array == null) {
         return null;
      } else if (array.length == 0) {
         return new Integer[] {};
      }

      final Integer[] result = new Integer[array.length];
      for (int i = 0; i < array.length; i++) {
         result[i] = Integer.valueOf(array[i]);
      }
      return result;
   }

   public static void main(String args[]) {
      int[] arr = { 1, 2, 3, 1, 4, 5 };
      Integer[] newArr = toObject(arr);
      System.out.println(arr.getClass().getName());
      System.out.println(newArr.getClass().getName());
   }
}

Output:

[ I
[ Ljava.lang.Integer;

Based on the above program we can convert any type of primitive array to its wrapper type array. Now, let us see an another program to convert double array to Double[ ].

Program for Java Primitive Array To Wrapper Array – double array to Double array

import java.util.Arrays;

public class Main {
   public static Double[] toObject(double[] array) {
      if (array == null) {
         return null;
      } else if (array.length == 0) {
         return new Double[] {};
      }

      final Double[] result = new Double[array.length];
      for (int i = 0; i < array.length; i++) {
         result[i] = Double.valueOf(array[i]);
      }
      return result;
   }

   public static void main(String args[]) {
      double[] arr = { 1.2, 3.22, 10.33, 4.22, 5.33 };
      Double[] newArr = toObject(arr);
      System.out.println(arr.getClass().getName());
      System.out.println(newArr.getClass().getName());
   }
}

Output:

[ D
[ Ljava.lang.Double;

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published.