Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this short tutorial, we’ll show how to convert an array of primitives to an array of objects, and vice versa.

2. Problem

Let’s say we have an array of primitives, such as int[], and we would like to convert it to an array of objects, Integer[]. We might intuitively try casting:

Integer[] integers = (Integer[])(new int[]{0,1,2,3,4});

However, this will result in a compilation error because of inconvertible types. That’s because autoboxing only applies to individual elements and not to arrays or collections.

Therefore, we need to convert the elements one by one. Let’s take a look at a couple of options to do that.

3. Iteration

Let’s see how we can use autoboxing in an iteration.

First, let’s convert from a primitive array to an object array:

int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };

Integer[] output = new Integer[input.length];
for (int i = 0; i < input.length; i++) {
    output[i] = input[i];
}

assertArrayEquals(expected, output);

Now, let’s convert from an array of objects to an array of primitives:

Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };

int[] output = new int[input.length];
for (int i = 0; i < input.length; i++) {
    output[i] = input[i];
}

assertArrayEquals(expected, output);

As we can see, this is not complicated at all, but a more readable solution, like the Stream API, might suit our needs better.

4. Streams

Since Java 8, we can use the Stream API to write fluent code.

First, let’s see how to box the elements of a primitive array:

int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };

Integer[] output = Arrays.stream(input)
  .boxed()
  .toArray(Integer[]::new);

assertArrayEquals(expected, output);

Notice the Integer[]::new parameter in the toArray method. Without this parameter, the stream would return an Object[] instead of the Integer[].

Next, to convert them back, we’ll use the mapToInt method together with the unboxing method of Integer:

Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };

int[] output = Arrays.stream(input)
  .mapToInt(Integer::intValue)
  .toArray();

assertArrayEquals(expected, output);

With the Stream API, we created a more readable solution, but if we still wish it were more concise, we could try a library, like Apache Commons.

5. Apache Commons

First, let’s add the Apache Commons Lang library as a dependency:

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

Then, to convert a primitives array to its boxed counterpart, let’s use the ArrayUtils.toObject method:

int[] input = new int[] { 0, 1, 2, 3, 4 };
Integer[] expected = new Integer[] { 0, 1, 2, 3, 4 };

Integer[] output = ArrayUtils.toObject(input);

assertArrayEquals(expected, output);

Lastly, to convert back the boxed elements to primitives, let’s use the ArrayUtils.toPrimitives method:

Integer[] input = new Integer[] { 0, 1, 2, 3, 4 };
int[] expected = new int[] { 0, 1, 2, 3, 4 };

int[] output = ArrayUtils.toPrimitive(input);

assertArrayEquals(expected, output);

The Apache Commons Lang library provides a concise, easy-to-use solution to our problem, with the cost of having to add a dependency.

6. Conclusion

In this article, we’ve looked at a couple of ways to convert an array of primitives to an array of their boxed counterparts, and then, convert the boxed elements back to their primitive counterparts.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW