Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Lambda & functional interface

 
Ranch Hand
Posts: 80
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

To my knowledge about lambda expression, the method signature Print() must match with the abstract method inside the functional interface.
But if I have a method that has different return type than the abstract method in the functional interface, it also works.
Please advise.

Thanks!

 
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because line 9 essentially compiles to this (a similar thing happens on line 11):

So the int returned by Print() is not used at all.

Try declaring MyInterface.test() to return an int instead of void and then assign the result of p.test() to an int var or just simply do:

You'll realize that lambdas and method references will "shape shift" to fit whatever they're being assigned to (this happens at compile time).
 
Marshal
Posts: 69847
278
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please explain what you mean about method signatures. Remember that the return type isn't part of the method signature.

Please only use // comments for something short. Your long comments inside code tags have made the lines too long to read easily.
 
henry leu
Ranch Hand
Posts: 80
3
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Junilu Lacar wrote:Because line 9 essentially compiles to this:

So the int returned by Print() is not used at all.

Try declaring MyInterface.test() to return an int instead of void and then assign the result of p.test() to an int var or just simply do:

You'll realize that lambdas and method references will "shape shift" to fit whatever they're being assigned to (this happens at compile time).



Thank you Junilu!

But based on your explaination, I still don't get the point.

MyInterface p = () -> (4+5);  // (4+5) evaluated to int. This would not compile. OKAY
MyInterface p = () -> ( Print() + Print() ); // Print() + Print() evaluates to int. This would not compile. OKAY
MyInterface p = () -> Print();  // But Print() evaluates to int too. Why does this compile?
 
henry leu
Ranch Hand
Posts: 80
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Please explain what you mean about method signatures. Remember that the return type isn't part of the method signature.

Please only use // comments for something short. Your long comments inside code tags have made the lines too long to read easily.



Hi Campbell,

I will take your advise for my future post.

What I meant for method signature is that the method parameter and method return type.

 
Saloon Keeper
Posts: 12154
258
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For a lambda expression to be compatible with a functional interface that has no return type, the lambda expression body must be a statement expression.

These are not statement expressions. They are additive expressions.

A simple method call is a statement expression. The return type of the method is ignored.

henry leu wrote:What I meant for method signature is that the method parameter and method return type.


Then say "parameter list and return type". Return types are not part of the method signature and the method name is not relevant to lambda expressions. It's important to be precise in programming.
 
Campbell Ritchie
Marshal
Posts: 69847
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

henry leu wrote:. . . What I meant for method signature is that the method parameter and method return type.

But the method signature is its name, equivalence of any formal type parameters <T, U, V> and the types of its parameter list. It is defined in the Java® Language Specification (=JLS). As Stephan says (and I sometimes say it too) you need to be precise with your jargon.
 
Junilu Lacar
Sheriff
Posts: 15801
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

henry leu wrote:
MyInterface p = () -> (4+5);  // (4+5) evaluated to int. This would not compile. OKAY
MyInterface p = () -> ( Print() + Print() ); // Print() + Print() evaluates to int. This would not compile. OKAY
MyInterface p = () -> Print();  // But Print() evaluates to int too. Why does this compile?


Another way to look at it would be to plug in the body of your lambda in the equivalent anonymous class declaration:

So, substituting (4 + 5) on line 4:

And substituting Print() + Print() on line 4:

And finally, substituting Print() on line 4:

Again, I'd encourage you to experiment and see if you had this declaration instead:

and tried it with the same lambda expressions you've been using. When you declare it this way, the equivalent anonymous class code will be slightly different.
 
Good heavens! What have you done! Here, try to fix it with this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic