Why Java-8 requires lambda invokeDynamic byteCode to call an interface method

Hi, I'm trying to understand the benefits of using a Lambda expression, where I heard that using invokeDynamic byteCode by JVM to execute a lambda expression will improve performance compared to XXXX (sorry, I don't know, it could be an anonymous inner class).

My question is: why do Lambda expressions require 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 a lambda expression (int a, int b) -> a + b;can simply be removed in a static method, for example

   private static int lambda$1(int a, int b) {
       return a + b;
   }

which can ultimately be called using invokstaticbyteCode on the right?

Other questions: what is the dynamic lambda expression that it tries to achieve when the entire type of the argument returns the type defined during compilation itself.

: lambda (int a, int b) -> a + b; (argument type, return type) ?

+4
1

, -, invokedynamic. invokeinterface, . , , .

, JRE, (invokestatic, invokevirtual invokespecial). , .. , , private.

invokedynamic , , , , . , .

+10

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


All Articles