range(start, end) stops one short of end while rangeClosed(start, end) includes it, a distinction easy to miss since both otherwise behave identically.
int[] exclusive = IntStream.range(1, 5).toArray();
int[] inclusive = IntStream.rangeClosed(1, 5).toArray();
System.out.println("range(1,5): " + Arrays.toString(exclusive));
System.out.println("rangeClosed(1,5): " + Arrays.toString(inclusive));
range(1,5): [1, 2, 3, 4]
rangeClosed(1,5): [1, 2, 3, 4, 5]
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