Hi I am trying to understand the advantages of using Lambda expression, where I heard usage of the invokeDynamic byteCode by JVM to execute lambda expression will improve the performance compared to XXXX(Sorry I don't know, it might be an Anonymous inner class).
My question is Why Lambda expressions need invokeDynamic byteCode.
For example:
public class LambdaTest {
public static void main(String[] args) {
MathOperation addition = (int a, int b) -> a + b;
addition.operation(1, 2);
}
private static int operate(int a, int b, MathOperation operation){
return operation.operation(a, b);
}
interface MathOperation {
int operation(int a, int b);
}
}
where the lambda expression (int a, int b) -> a + b;
can be simply desugared into a static method like
private static int lambda$1(int a, int b) {
return a + b;
}
which eventually can be invoked using invokstatic
byteCode right?
More questions: What is the dynamicity lambda expression trying to achieve, when all the argument type, returns type are defined during the compile time itself.
For example: in the lambda
expression (int a, int b) -> a + b;
everything(argument type, return type
) got defined during the compile time itself right?