Streams: IntStream.range excludes the end, rangeClosed includes it

range(start, end) stops one short of end while rangeClosed(start, end) includes it, a distinction easy to miss since both otherwise behave identically.

Code
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));
Output
range(1,5): [1, 2, 3, 4]
rangeClosed(1,5): [1, 2, 3, 4, 5]
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