What I meant was that lambda expressions are just syntactic sugar. The JVM doesn't understand lambda expressions.
Conceptually, the compiler simply converts the given lambda expression into some class, instantiates it, and passes that object to the method. While doing so, it follows certain process and rules. It puts the part after -> as the method body.
Let's see this now:
1. ls.stream().forEach((x)->process( MyProcessor::new ));
forEach expects an object of a class that implements Consumer. So, the compiler uses the code given in the lambda expression and creates this class and an object of that class. For example, in this case, it will do:
Code: Select all
class SomeMadeUpClassName implements Consumer{
public void accept(Integer x){
process(new MyProcessor()); //this is illegal because you cannot invoke MyProcessor's process method like this.
}
}
What you really want in the body of the above accept method is this line of code: new MyProcessor(x).process();
Please note that this is a long topic to discuss in a post but I have tried to explain it in brief. You need go through a good book to understand how lambdas and method references work.