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?
Asked
Active
Viewed 9,495 times
5
-
1That's a [Lambda Expression](http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html). – Cᴏʀʏ Sep 17 '14 at 19:35
-
Have a look at the following http://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html, http://viralpatel.net/blogs/lambda-expressions-java-tutorial/ – CodeWarrior Sep 17 '14 at 19:37
1 Answers
7
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

Peter Lawrey
- 525,659
- 79
- 751
- 1,130
-
1
-
3Locals captured by anonymous inner classes need only be effectively final, not explicitly declared final. This is similar to lambdas and is a relaxation in Java 8 from previous versions. Lambdas will also capture the enclosing instance, but only if something requires it to be captured. You're correct, though, non-capturing lambdas won't capture the enclosing instance, whereas AICs will even if it would arguably be unnecessary. – Stuart Marks Sep 17 '14 at 20:40
-
1As far as I know there is no requirement for the lambda implementation methods to be `static`. E.g. lambdas capturing this could be implemented using an instance method, however, current compilers don’t use this opportunity. – Holger Sep 18 '14 at 07:40