How does the arrow operator work inside java 8?

I understand that the left side of the arrow has arguments, and the right side of the arrow is a function in which the arguments go. But I would like to know how java 8 displays the left and right sides and is converted to a function. What happens there and where can I find information?

+5
source share
1 answer

When you have -> , the javac compiler adds a static method with the contents of the code. It also adds dynamic call information to the class, so the JVM can display the interface that the lambda implements for the arguments and return type. The JVM generates code at run time to bind the interface to the generated method.

The difference with lambdas and anonymous classes is that the implict variables should only be final (as it could be final), and the member variables are copied, that is, they do not retain the reference to this external class.

It can tell the difference between Runnable and Callable<void> , although both arguments do not accept. For more details http://vanillajava.blogspot.com/2014/09/lambdas-and-side-effects.html

+7
source

Source: https://habr.com/ru/post/1202809/


All Articles