O'Reilly logo

Java 11 Cookbook by Mohamed Sanaulla, Nick Samoylov

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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. ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required