This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Grokking Bitcoin and have Kalle Rosenbaum on-line!
See this thread for details.
Win a copy of Grokking Bitcoin this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Java 8 Lambda Expressions Confusions  RSS feed

 
Ranch Hand
Posts: 391
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose we have following functional interfaces in Java 8:


How the Lambda Expression will work in such cases? Will it match all the methods definitions and will find out the most exact match to use the method?
 
Marshal
Posts: 64109
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You would have to declare your λ reference as a particular type, either A or B. Actually, those examples are not very good; it is usual to use a λ to define a function, and methods with void instead of a return type are not functions.
 
Master Rancher
Posts: 3179
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What problems do you expect?

gives no problems. And try
 
Vaibhav Gargs
Ranch Hand
Posts: 391
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose we have the following piece of code:



Can we use lambda in this case to represent C? If yes, then how the method resolution happen? Does the compiler evaluate each method in the class to match the lambda syntax?
 
Piet Souris
Master Rancher
Posts: 3179
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you cannot have a class that implements both A and B, since that class would have to implement both 'void test()' and 'int test()', and these methods have the same signature.

What IS possible, is this:
(try it out and see if it answers your question)
 
Vaibhav Gargs
Ranch Hand
Posts: 391
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let us consider the following cases:

Scenario 1:



Scenario 2:


Can we use Lambdas here? If yes, then how it will identify the exact method call?
 
Saloon Keeper
Posts: 10107
212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No. A lambda expression can only return instances of a functional type. C is not a functional type, because it has more than one abstract method.
 
Stephan van Hulst
Saloon Keeper
Posts: 10107
212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please note that you can easily answer these questions for yourself by compiling and running code. You also would have spotted that your C class isn't valid because it's not abstract.
 
Vaibhav Gargs
Ranch Hand
Posts: 391
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Stephan. I apologise; by class C i meant we have the implementations in place:



In that case, can we use lambdas to refer to C? How the resolution will happen, if yes?
 
Stephan van Hulst
Saloon Keeper
Posts: 10107
212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If C already has implementations, why woud you need to write lambda expressions? Please show with a complete example what you mean.
 
Vaibhav Gargs
Ranch Hand
Posts: 391
2
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Stephan, I am just trying to understand the concept of Lambdas. So, as we can do behavior parameterization using lambdas, so, just thinking on those lines to provide an implementation at run time.
 
Piet Souris
Master Rancher
Posts: 3179
119
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well,

if your intention is this:

that is impossible, as Stephan writes, since C is a class ad not a Functional Interface. And implementing a class that implements both A and B is also impossible, as I wrote. But do prove me wrong: do you have a concrete implementation?

Last thing I can think of: what about

Try it!
 
Stephan van Hulst
Saloon Keeper
Posts: 10107
212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lamda's are pretty simple. If a variable (parameter, field or local) is of a type that has exactly one abstract method, you can assign the result of a lambda expression to it.

If a type has overloaded methods that accept a functional type, the overload that is chosen depends on the input parameters of the lambda expression, and the type of the return value.

An interesting question is what happens when you want to differentiate between two overloads that accept different functional types, but those functional types contain methods with the same parameter list and return type:
 
Lasagna is spaghetti flvored cake. Just like this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!