Consumer con = (s) -> System.out::println;
Here, you're trying to invoke the System.out.println()
with what we call method reference in Java 8. When you're to reference a method in lambda expression its must be of like this,
Consumer con = System.out::println;
You don't actually need the s
to call println
method. Method reference will take care of that. This ::
operator means you'll call the println
method with a parameter and you don't going to specify its name.
But when you do this,
Consumer con2 = (s) -> {System.out.println(s);};
you're telling the lambda expression to explicitly println the content of s
which is perfectly fine technically so it doesn't arise any compile error.