Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

lambda  RSS feed

 
Linwood Hayes
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


above code is from tutorialpoints   https://www.tutorialspoint.com/java8/java8_lambda_expressions.htm.

Question:  when we do 
MathOperation subtraction = (a, b) -> a - b;
it automatically finds that "operation(..,..)" method, but what if MathOperation has ANOTHER method with same signature but different method name ? How can lambda locate it ?

 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15985
86
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Then it will not work and you will get a compiler error.

MathOperation must be a functional interface - an interface with exactly one abstract method. When you assign a lambda expression to a variable of such an interface, the parameters and return type of the lambda must match the parameters and return type of the method in the functional interface.

There's an error in your code, in lines 17-20 you call the method operate but in the interface it's named operation (line 35).
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:There's an error in your code, in lines 17-20 you call the method operate but in the interface it's named operation (line 35).

That's a different method - not the functional interface method but the method at the bottom, which takes a MathOperation and simply delegates the call to it.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 15985
86
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah yes, I see, I was too quick...
 
I've never won anything before. Not even a tiny ad:
Thoughts on deprecation in Java
https://coderanch.com/t/683016/java/Deprecation-Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!