• Post Reply Bookmark Topic Watch Topic
  • New Topic

Lambda doubt  RSS feed

 
Anuraag Kumar
Greenhorn
Posts: 11
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have just started to explore Lambdas.

Can you please let me know why the below snippet gives the error - "Syntax error, insert "AssignmentOperator Expression" to complete Expression"

() -> System.out.println("print");
 
Piet Souris
Master Rancher
Posts: 2044
75
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An expression like the one mentioned needs some context to operate in. For instance:

would do fine.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A lambda is actually compiled into an implementation of a functional interface. Since by definition a functional interface contains a single method, the compiler can infer what method to invoke so under the covers, it will create an object with that single method and the body of the lambda as the method's body. In Piet's example, the functional interface that gives the lambda expression its context is the Runnable interface. You can use basically the same lambda expression in different contexts.

Here's an example that illustrates this:

The interfaces defined are all functional interfaces that define different methods. The lambda being assigned to each of the interface variables declared on lines 3, 6, 9, and 12 essentially have the same body except they are wrapped in different methods depending on the interface type the lambda expression is given. So, when you assign the expression to a Frog interface reference, you have to call the talk() method. If you assign the lambda to Displayer interface reference, you have to call the show() method, and so on.

The output of that program will be:

Foo!
Bar!
Ribbit!
Hello!

You can run that code from here: https://repl.it/MT4Z/0

Then try it with these lines added to main():
 
Anuraag Kumar
Greenhorn
Posts: 11
Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the explanation Junilu.

I believe ((Displayer) kermit).show(); won't compile as the reference kermit cannot be cast to Displayer.
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. This shows you that the type of the object that wraps the lambda expression is determined at compile time.

Sorry, got confused by my own example. Actually, the code results in a RuntimeException (ClassCastException)
 
Junilu Lacar
Sheriff
Posts: 11493
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's another example (saved at https://repl.it/MT4Z/3)

In this example, g is an instance of the Glom class and it implements both the Foo and Bar interfaces. With the proper casts you can invoke both foo() and bar() methods on the same object. Can you see why casting in this case works whereas it didn't work in the previous example?
 
Campbell Ritchie
Marshal
Posts: 56545
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anuraag Kumar wrote:. . . ((Displayer) kermit).show(); won't compile . . .
That sort of check is only made on classes at compile time. It is conceivable that a class might implement an interface, so casting of interfaces is allowed at compile time and as Junilu says, you suffer an exception at runtime.

Just shows what a bad idea it often is to cast reference types.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!