Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

arrayutils
java
primitive
communitycreator

What is Arrayutils.toPrimitive in Java?

abhilash

toPrimitive is a static method of the ArrayUtils class that converts the elements of the Object type to its corresponding primitive type.

The toPrimitive method is not an in-place method and returns a new array consisting of elements present in the passed array with the type converted from Object to primitive.

For example:

  • Input Array = Integer[] integers = {Integer.valueOf(34543234), Integer.valueOf(54342), Integer.valueOf(32)}

Given the array above of type Integer containing the object/boxed values of integer, the method converts the array elements to its primitive type: int.

The table below lists the primitive types and their corresponding Wrapper/Object classes.

Primitive type Object type
byte Byte
short Short
int Integer
long Long
float Float
double Double
boolean Boolean
char Character

ArrayUtils is defined in the Apache Commons Lang package. To add Apache Commons Lang to the Maven Project, add the following dependency to the pom.xml file.

<dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.12.0</version>
</dependency>

For other versions of the commons-lang package, refer to the Maven Repository.

You can import the ArrayUtils class as follows.

import org.apache.commons.lang3.ArrayUtils;

Syntax

public static int[] toPrimitive(final Integer[] array, final int valueForNull)

Parameters

  • final Integer[] array: the array of type Integer
  • final int valueForNull: the integer value to replace if null values are found in the array (optional)

Return value

A new array consisting of the array elements of their corresponding primitive type.

Code

import org.apache.commons.lang3.ArrayUtils;

public class Main {

    public static void main(String[] args) {

        Integer[] integers = {Integer.valueOf(2343), Integer.valueOf(33), Integer.valueOf(234), null};
        int[] intResult = ArrayUtils.toPrimitive(integers, 23);
        assert intResult instanceof int[];

        Double[] doubles = {Double.valueOf(34543234), Double.valueOf(54342), Double.valueOf(32)};
        double[] doubleResult = ArrayUtils.toPrimitive(doubles);
        assert doubleResult instanceof double[];

    }
}

Example 1

  • Input array - Integer[] integers = {Integer.valueOf(2343), Integer.valueOf(33), Integer.valueOf(234), null};

The assert statement is used to check if the return value of the ArrayUtils.toPrimitive for the above array of Integer type is an array of primitive type (i.e, int).

Example 2

  • Input array - Double[] doubles = {Double.valueOf(34543234), Double.valueOf(54342), Double.valueOf(32)};

The assert statement is used to check if the return value of the ArrayUtils.toPrimitive for the above array of Double type is an array of primitive type (i.e, double).

RELATED TAGS

arrayutils
java
primitive
communitycreator
RELATED COURSES

View all Courses

Keep Exploring