Lambda expression is another way to write an instance of an anonymous class to make an anonymous class instance easier to write. In the JVM, it will not take up much memory compared to the usual creation of Java objects using new (execution of static variables, static blocks, loading classes from a single hierarchy).

Lambda expression syntax:
(params) -> expression to implement a @FunctionalInterface
In your test case: String s = (String) invoke(() -> true); the expression has a return type of true with no argument. Thus, Runnable FunctionalInterface does not match the lambda expression because it has the void run() attribute. It matches Callable FuncationalInterface using V call() .
How do lambda expressions work under the hood?
It may seem that lambda expressions are just syntactic sugar for anonymous inner classes, but there is a much more elegant approach. The simplest explanation: the lambda expression is represented by a new method and is invoked at run time using invokedynamic.
Source:
class LambdaExample { public void abc() { Runnable r = () -> { System.out.println("hello"); } r.run(); } }
Bytecode equivalent:
class LambdaExample { public void abc() { Runnable r = <lambda$1 as Runnable instance>; r.run(); } static void lambda$1() { System.out.println("hello"); } }
Inside the JVM, there is a lambda factory that creates an instance of a functional interface (e.g. Runnable) from the generated lambda method (e.g. lambda $ 1).
Lambda expressions are great, and in Java 8 ...
Premraj
source share