How to do it...

  1. Experiment with the range(int startInclusive, int endInclusive) and rangeClosed(int startInclusive, int endInclusive) methods of the IntStream and LongStream interfaces:
IntStream.range(1,3).forEach(System.out::print); //prints: 12LongStream.range(1,3).forEach(System.out::print); //prints: 12IntStream.rangeClosed(1,3).forEach(System.out::print);  // 123LongStream.rangeClosed(1,3).forEach(System.out::print); // 123

As you can see, the difference between the range() and rangeClosed() methods is the exclusion or inclusion of the value passed in as the second parameter. This also leads to the following results in the case where both parameters have the same value:

IntStream.range(3,3).forEach(System.out::print);                                                //prints:LongStream. ...

Get Java 11 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.