Reputation: 3969
Is it true my statement?
There is my code:
public void start() {
Consumer<Integer> someFunc = (someInt) -> {
System.out.println("Hello lambda!");
};
}
And there is bytecode of my code:
~ Start method
// access flags 0x1
public start()V
L0
LINENUMBER 9 L0
INVOKEDYNAMIC accept()Ljava/util/function/Consumer; [
// handle kind 0x6 : INVOKESTATIC
java/lang/invoke/LambdaMetafactory.metafactory(Ljava/lang/invoke/MethodHandles$Lookup;Ljava/lang/String;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodType;Ljava/lang/invoke/MethodHandle;Ljava/lang/invoke/MethodType;)Ljava/lang/invoke/CallSite;
// arguments:
(Ljava/lang/Object;)V,
// handle kind 0x6 : INVOKESTATIC
me/alexandr/SomeMainClass.lambda$start$0(Ljava/lang/Integer;)V,
(Ljava/lang/Integer;)V
]
ASTORE 1
L1
LINENUMBER 12 L1
ALOAD 1
ICONST_1
INVOKESTATIC java/lang/Integer.valueOf (I)Ljava/lang/Integer;
INVOKEINTERFACE java/util/function/Consumer.accept (Ljava/lang/Object;)V
L2
LINENUMBER 13 L2
RETURN
L3
LOCALVARIABLE this Lme/alexandr/SomeMainClass; L0 L3 0
LOCALVARIABLE someFunc Ljava/util/function/Consumer; L1 L3 1
// signature Ljava/util/function/Consumer<Ljava/lang/Integer;>;
// declaration: java.util.function.Consumer<java.lang.Integer>
MAXSTACK = 2
MAXLOCALS = 2
~ Translated lambda expression
// access flags 0x100A
private static synthetic lambda$start$0(Ljava/lang/Integer;)V
L0
LINENUMBER 10 L0
GETSTATIC java/lang/System.out : Ljava/io/PrintStream;
LDC "Hello lambda!"
INVOKEVIRTUAL java/io/PrintStream.println (Ljava/lang/String;)V
L1
LINENUMBER 11 L1
RETURN
L2
LOCALVARIABLE someInt Ljava/lang/Integer; L0 L2 0
MAXSTACK = 2
MAXLOCALS = 1
As far as I can see - a lambda expression translated into a static method. So, can I say that functional interface is "lever" for the static method that allow me to call it?
Upvotes: 1
Views: 272
Reputation: 533530
If you use the ->
notation, the actual method implementing the code is placed in a `static method. The lambda created at runtime will call this method.
If you use the ::
notation, there is no additional method required and the lambda will be generated at runtime to call the ::method
So, can I say that functional interface is "lever" for the static method that allow me to call it?
You can call it a "lever" if you like. It might be called a bridging functions. As @Dici points out the term javap
uses is "bootstrap method" Note: these bootstrap methods are stored as Strings in the meta-data and don't appear as Method
in reflection or in the byte code via ASM (the library the JVM uses internally) ASM sees them as meta data.
Upvotes: 4