Streams: Stream.of a single element differs from Arrays.stream on an array

Stream.of(T...) unpacks an Object array like String[] as varargs, but int[] is not an Object[], so Stream.of(int[]) treats the whole array as one element; Arrays.stream(int[]) is the one that correctly unpacks it into an IntStream.

Code
String[] words = {"a", "b", "c"};
long fromOf = Stream.of(words).count();
System.out.println("Stream.of(String[]) unpacks via varargs, count: " + fromOf);
int[] primitives = {1, 2, 3};
Stream<int[]> single = Stream.of(primitives);
System.out.println("Stream.of(int[]) makes ONE element, count: " + single.count());
IntStream unpacked = Arrays.stream(primitives);
System.out.println("Arrays.stream(int[]) unpacks it, count: " + unpacked.count());
Output
Stream.of(String[]) unpacks via varargs, count: 3
Stream.of(int[]) makes ONE element, count: 1
Arrays.stream(int[]) unpacks it, count: 3
Advertisement
More in JAVA

Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.

Published 2026-09-27

© Java Coding Hub · About · Contact · Privacy · Terms