I do not understand one thing, should not the type of the method reference be a functional interface? So how do we pass System.out::println as a parameter to forEach?in the following code :
1-is System.out::println a Consumer?
2-if yes what is its generic type? I do not mean the type of list that is calling the method, but the generic type of the method reference that passed to the forEach.
Consumer<Integer> consumer=System.out::println;
numbers.forEach(consumer);
This part of code:
Consumer<Integer> consumer=System.out::println;
is equal to this lambda expression:
Consumer<Integer> consumer=i->System.out.println(i);
and above code block could be written in this way as an anonymous inner class using Consumer interface:
Consumer<Integer> consumer=new Consumer<Integer>(){
public void accept(Integer i){
System.out.println(i);
}
}
So "System.out.println" itself it is not a functional interface, but when you use :: you implicitly implement the Consumer interface.