What is the use of “System.out::println” in Java 8 [duplicate]

It’s called a “method reference” and it’s a syntactic sugar for expressions like this:

numbers.forEach(x -> System.out.println(x));

Here, you don’t actually need the name x in order to invoke println for each of the elements. That’s where the method reference is helpful – the :: operator denotes you will be invoking the println method with a parameter, which name you don’t specify explicitly:

numbers.forEach(System.out::println);

Leave a Comment