Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the arrow operator work internally in java 8? [duplicate]

Tags:

java

java-8

I understand that left side of the arrow has the arguments and right side of the arrow is the function where the arguments go to. But, I would like to know how does java 8 map the left side and right side and convert into a Function. What happens there and where can I find the information?

like image 308
would_like_to_be_anon Avatar asked Sep 17 '14 19:09

would_like_to_be_anon


1 Answers

When you have an -> the javac compiler adds a static method with the contents of the code. It also adds dynamic call side information to the class so the JVM can map the interface the lambda implements to the arguments and return type. The JVM generates code at runtime to bind the interface to the generated method.

A difference with lambdas and anonymous classes is that implict variables only need to be effectively final (as in could have been made final) and member variables are copied i.e. it doesn't keep a reference to the this of an outer class.

It can tell the difference between Runnable and Callable<void> even though both take no arguments. For more details http://vanillajava.blogspot.com/2014/09/lambdas-and-side-effects.html

like image 151
Peter Lawrey Avatar answered Sep 25 '22 12:09

Peter Lawrey