How to pass an array of primitives as varargs?

The String.format(String format, Object... args) is waiting an Object varargs as parameter. Since int is a primitive, while Integer is a java Object, you should indeed convert your int[] to an Integer[].

To do it, you can use nedmund answer if you are on Java 7 or, with Java 8, you can one line it:

Integer[] what = Arrays.stream( data ).boxed().toArray( Integer[]::new );

or, if you don't need to have an Integer[], if an Object[] is enough for your need, you can use:

Object[] what = Arrays.stream( data ).boxed().toArray();


You can use the wrapper class Integer instead, i.e.

System.out.println(String.format("%s %s", new Integer[] { 1, 2 }));

This is how you would cast an existing int[] array:

int[] ints = new int[] { 1, 2 };

Integer[] castArray = new Integer[ints.length];
for (int i = 0; i < ints.length; i++) {
    castArray[i] = Integer.valueOf(ints[i]);
}

System.out.println(String.format("%s %s", castArray));