what is the inferred type of the method reference System.out::println?

Description:

IntStream.range(0, 10).forEach(System.out::println);

Solution:

It is an IntConsumer.

IntStream.range(0, 10) returns an IntStream, and IntStream defines a .forEach() method accepting an IntConsumer as an argument, whose prototype is:

void accept(int value);

System.out is a PrintStream, and a PrintStream has a method named println which takes an int as an argument and returns void. This matches the signature of an IntConsumer, hence the result.

0 answers