• 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:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Java 8 Lambda Expressions Confusions  RSS feed

 
Ranch Hand
Posts: 340
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: 60136
188
  • 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.
 
Rancher
Posts: 2834
96
  • 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: 340
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
Rancher
Posts: 2834
96
  • 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: 340
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: 9239
177
  • 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: 9239
177
  • 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: 340
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: 9239
177
  • 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: 340
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
Rancher
Posts: 2834
96
  • 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: 9239
177
  • 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:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!