... System.out::println
in this case is a method reference—a shorthand notation for a lambda that calls the specified method. A method reference of the form
objectName::
instanceMethodName
is a bound instance method reference—“bound” means the specific object to the left of the ::
(System.out
) must be used to call the instance method to the right of the ::
(println
). The compiler converts System.out::println
into a one-parameter lambda like
x -> System.out.println(x)
that passes the lambda’s argument—the current stream element (represented by x
)—to the System.out
object’s println
instance method, which implicitly outputs the String
representation of the argument. The stream pipeline of lines 12–13 is equivalent to the following for
loop:
for (int i = 1; i <= 10 ...
Get Java How to Program, Early Objects, 11th Edition 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.