Public interface ANIMAL {
Void cry ();
Public static void main (String [] args) {
ANIMAL cat=() - & gt; System. The out. Println (" cat: mews ");
The Object o=cat;//true
//Object o1=() - & gt; System. The out. Println (" cat: mews ");//error
}
}
The code above
See "Java core technology volume 1", said that cannot be directly assigned to the lambda expressions directly to the Object, I also test, really not line,
But why use Main cat=() - & gt; System. The out. Println (" cat: mews "); Later, ok? Is the Main cat=() - & gt; System. The out. Println (" cat: mews "); This line of code that can put the lambda expressions to automatically generate a cat object?
I also read the bytecode:
Public static void main (Java. Lang. String []);
Descriptor: ([Ljava/lang/String;) V
Flags: ACC_PUBLIC, ACC_STATIC
Code:
Stack=1, locals=3, args_size=1
0: invokedynamic # 1, # 0:0//invokedynamic cry: () Lcom/company/Main;
5: astore_1
6: aload_1
7: astore_2
8: return
LineNumberTable:
Line 7:0
Line 8:6
Line 10:8
I don't see related to generate the object's instruction, I checked the invokedynamic instruction and generate object has nothing to do, also
This I have bit of doubt, the book also said, don't put the lambda expressions as the object, to as a block of code, that why this will happen
CodePudding user response:
From the top! Can't sink!!!!!!
CodePudding user response:
refer to the original poster crazy made the wheels of programming enthusiasts reply: this I have bit of doubt, the book also said, don't put the lambda expressions as the object, to as a block of code, why will appear this situation To don't want to instantiate your assignment? //instantiate an Animal interface Animal cat=() - & gt; System. The out. Println (" cat: mews "); //the instantiation of the Animal interface type cat assigned to the Object type The Object o=cat; The Main cat=() - & gt; System. The out. Println (" cat: mews "); You can compile? Unless you are the Main this interface type is not pass, Did have a good look at the program into the Main, CodePudding user response:
refer to the second floor qq_39936465 response: Quote: refer to the original poster crazy made the wheels of programming enthusiasts reply: This I have bit of doubt, the book also said, don't put the lambda expressions as the object, to as a block of code, why will appear this situation To don't want to instantiate your assignment? //instantiate an Animal interface Animal cat=() - & gt; System. The out. Println (" cat: mews "); //the instantiation of the Animal interface type cat assigned to the Object type The Object o=cat; The Main cat=() - & gt; System. The out. Println (" cat: mews "); You can compile? Unless you are the Main this interface type is not pass, Did have a good look at the program into the Main, ANIMAL which has the Main cat cat ah CodePudding user response:
refer to the second floor qq_39936465 response: Quote: refer to the original poster crazy made the wheels of programming enthusiasts reply: This I have bit of doubt, the book also said, don't put the lambda expressions as the object, to as a block of code, why will appear this situation To don't want to instantiate your assignment? //instantiate an Animal interface Animal cat=() - & gt; System. The out. Println (" cat: mews "); //the instantiation of the Animal interface type cat assigned to the Object type The Object o=cat; The Main cat=() - & gt; System. The out. Println (" cat: mews "); You can compile? Unless you are the Main this interface type is not pass, Did have a good look at the program into the Main, refer to the second floor qq_39936465 response: Quote: refer to the original poster crazy made the wheels of programming enthusiasts reply: This I have bit of doubt, the book also said, don't put the lambda expressions as the object, to as a block of code, why will appear this situation To don't want to instantiate your assignment? //instantiate an Animal interface Animal cat=() - & gt; System. The out. Println (" cat: mews "); //the instantiation of the Animal interface type cat assigned to the Object type The Object o=cat; The Main cat=() - & gt; System. The out. Println (" cat: mews "); You can compile? Unless you are the Main this interface type is not pass, Did have a good look at the program into the Main, I adjusted my doubts In fact, I doubt is a lambda expression is not an object, then also how to perform assigned to an interface instance reference: Animal cat=() - & gt; System. The out. Println (" cat: mews "); Lambda expressions can be automatically converted into interface instance? When I tried: Object o1=() - & gt; System. The out. Println (" cat: mews "); This code is executed, give prompt is: Error: (9, 21) Java: incompatible types: Java. Lang. Object is not the function interface My understanding is that () - & gt; System. Out.println (" cat: mews ") is a function interface So this is lambda expressions can be directly converted into interface instance? How did this transformation is that,, CodePudding user response:
The crazy made the wheels of programming enthusiasts reference 4 floor response: I adjusted my doubts In fact, I doubt is a lambda expression is not an object, then also how to perform assigned to an interface instance reference: Animal cat=() - & gt; System. The out. Println (" cat: mews "); Lambda expressions can be automatically converted into interface instance? When I tried: Object o1=() - & gt; System. The out. Println (" cat: mews "); This code is executed, give prompt is: Error: (9, 21) Java: incompatible types: Java. Lang. Object is not the function interface My understanding is that () - & gt; System. Out.println (" cat: mews ") is a function interface So this is lambda expressions can be directly converted into interface instance? How did this transformation is that,, Representative methods () - & gt; Represents how to implement the method, in fact is equal to rewrite the method, CodePudding user response:
Lambda expressions is the manner in which a is used to implement interface, this interface must contain only one abstract method, What you need to know before using lambda expressions is the functional interface, need to know the origin of the lambda CodePudding user response:
refer to 6th floor sotondolphin response: lambda expressions is the manner in which a is used to implement interface, this interface must contain only one abstract method, What you need to know before using lambda expressions is the functional interface, need to know the origin of the lambda Is implemented an interface, then use the lambda implements this interface, why can be directly assigned to the interface instance? Animal cat=() - & gt; System. The out. Println (" cat: mews "); Is this example, the "() - & gt; System. Out.println (" cat: mews ") "means is an object? nullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnullnull