Quick revision
The short answer
It returns 1. Generics cannot hold primitives, so the varargs parameter T... of Arrays.asList cannot accept the ints one by one; instead the whole int[] becomes a single element, T is inferred as int[], and the result is a List<int[]> of size one. To get a List<Integer> we box the values, for example Arrays.stream(arr).boxed().toList(); Arrays.asList works as expected only with an Integer[], an array of objects.
What to remember
In this answer, we saw that Arrays.asList(new int[]{1, 2, 3}).size() returns 1, because type parameters cannot be primitives, so T is inferred as int[] and the whole array becomes the single element of a List<int[]>. With an Integer[], T is Integer and we get a view of three elements. To build a List<Integer> from an int[] we box with Arrays.stream(arr).boxed().toList() or a loop, and for large numeric data we keep the primitive array, since boxing costs memory and CPU.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
It returns 1. Generics cannot hold primitives, so the varargs parameter T... of Arrays.asList cannot accept the ints one by one; instead the whole int[] becomes a single element, T is inferred as int[], and the result is a List<int[]> of size one. To get a List<Integer> we box the values, for example Arrays.stream(arr).boxed().toList(); Arrays.asList works as expected only with an Integer[], an array of objects.
Why this question is asked
This is one of Java's best-known puzzles, and it is not just trivia: the same confusion produces real bugs, such as contains checks that always return false and lists that print as [[I@1b6d3586]. Interviewers ask it to see whether we understand how generics, varargs and primitives interact, and whether we know the correct conversions and their cost. In this answer, we will reproduce the surprise, explain exactly how the compiler infers T, look at the conversions that work, and finish with when to keep the data as a primitive array instead.
1Reproducing the Surprise
Here is the code from the question, along with what we might have expected.
The odd printout [I@1b6d3586 is the default toString of an array: [I is the JVM's name for an int array, followed by a hash code. Notice that var list = Arrays.asList(new int[]{1, 2, 3}) would hide the type, and the bug would show up only when contains(1) returns false or size() is wrong.
2Why T Becomes int[]
Arrays.asList is declared as public static <T> List<T> asList(T... a). A varargs parameter T... is really an array parameter T[]: the caller can pass separate values, which the compiler packs into a new array, or pass an existing array of type T[] directly.
Type parameters such as T can only stand for reference types, never primitives like int. So when we pass an int[], the compiler cannot choose T = int and treat the int[] as a T[], because int[] is not an array of a reference type. The only consistent choice is T = int[]: an int[] is itself an object, so it counts as a single T, and the compiler packs it into a new int[][] of length one. The list's single element is our array.
With an Integer[], T = Integer works, and Integer[] is exactly T[], so the compiler passes the array directly, and the list is a view of its three elements.
- Argument is an array of a reference type?e.g. Integer[], String[]YesT = element type, array passed as T[]NoT = the array type itself
- int[] is not a T[] for any Tprimitives cannot be type arguments
- T = int[]the array is one object, one element
- Pack into int[][] of length 1result is List<int[]> of size 1
4 steps in order. Play them, or step through them one at a time.
3Getting a List<Integer> From an int[]
To get a list of numbers, each int has to be boxed into an Integer, because a List can only hold objects. Streams make this short.
| Input | Call | Result |
|---|---|---|
| int[] {1, 2, 3} | Arrays.asList(arr) | List<int[]>, size 1 |
| int[] {1, 2, 3} | Arrays.stream(arr).boxed().toList() | List<Integer> [1, 2, 3], unmodifiable |
| int[] {1, 2, 3} | List.of(arr) | List<int[]>, size 1, same trap |
| Integer[] {1, 2, 3} | Arrays.asList(arr) | fixed-size view [1, 2, 3] |
| Integer[] {1, 2, 3} | List.of(arr) | immutable copy [1, 2, 3] |
Arrays.stream(int[]) gives an IntStream, a stream of primitive ints, and boxed() turns it into a Stream<Integer>. toList() returns an unmodifiable list; if we need to add or remove later, we collect into an ArrayList instead, or use the plain loop, which is also the fastest of the three for large arrays. Note the difference from Arrays.asList(Integer[]): those conversions create a new, independent list, while Arrays.asList returns a fixed-size view that writes through to the array.
4The Cost of Boxing
Boxing is not free. Each Integer is a separate object of about 16 bytes, plus a 4-byte reference in the list's array, so a list of a million ints takes around 20 MB against 4 MB for the int[]. Creating those objects also costs CPU and garbage collection time, and reading them means following a pointer per element. Integer.valueOf caches the values -128 to 127, so small numbers share objects, but most real data falls outside that range.
So for large numeric data we keep the primitive array and work on it directly, with IntStream for aggregate operations, or use a primitive collection library such as fastutil or Eclipse Collections when we need a growable list of ints. We convert to List<Integer> at the edges, where an API needs a List, and only for data of moderate size.
5Follow-up questions
Does List.of(arr) have the same problem with an int[]?
Yes. List.of has a varargs overload <E> List<E> of(E... elements), and for an int[] the only possible E is int[], so List.of(arr) is a List<int[]> with one element. With an Integer[] it creates an immutable list of the three values.
Is Arrays.asList(Integer[]) a copy?
No. It returns a fixed-size list backed by the array: set changes the array and changes to the array are visible in the list, while add and remove throw UnsupportedOperationException. List.of(integerArray) or List.copyOf(Arrays.asList(integerArray)) gives an independent immutable copy.
How do we go back from a List<Integer> to an int[]?
With list.stream().mapToInt(Integer::intValue).toArray(). mapToInt unboxes each element, and it throws NullPointerException if the list contains a null, since an int cannot be null.
6Conclusion
In this answer, we saw that Arrays.asList(new int[]{1, 2, 3}).size() returns 1, because type parameters cannot be primitives, so T is inferred as int[] and the whole array becomes the single element of a List<int[]>. With an Integer[], T is Integer and we get a view of three elements. To build a List<Integer> from an int[] we box with Arrays.stream(arr).boxed().toList() or a loop, and for large numeric data we keep the primitive array, since boxing costs memory and CPU.
Open with
It returns one, not three. Arrays.asList is declared as a generic varargs method, asList(T... a), and a varargs parameter is really an array parameter T[]. Type parameters can only be reference types, so T cannot be int, and an int array is not a T array for any T.
Then, if they want more
The only type the compiler can infer is T equals int[]: the whole array is one object, so it becomes a single element, and the result is a List of int[] with size one. Printing it shows something like [[I@1b6d3586], and contains(1) returns false, which is how this bug usually shows up.
With an Integer array it works as people expect, because T is Integer and the Integer[] is passed straight through, giving a fixed-size view of three elements that writes through to the array. List.of has exactly the same trap with an int array. To get a List of Integer from an int array, each value has to be boxed.
The neat way is Arrays.stream(arr).boxed().toList(), which gives an unmodifiable list, or collect into an ArrayList if I need to modify it, or just a loop into a pre-sized ArrayList.
I would also mention the cost: each Integer is a separate object of around sixteen bytes plus a reference, so a million values take about five times the memory of the int array and add pointer chasing. For large numeric data I keep the int array and use IntStream or a primitive collection library, and box only at API edges.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 16
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Collections Framework, overview, java.util, package documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "What does Arrays.asList(new int[]{1, 2, 3}).size() return, and why? How do you get a...": https://scrivio-interview-library.netlify.app/java/lists/8c1e05b071/