Win a copy of Mastering Corda: Blockchain for Java Developers 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
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why is void return type compatible with other datatypes in lambdas?

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
A new member here.
I have a query, that i was wondering if someone could help out with.

1)Here, the return type of single abstract method (SAM) of functional interface A is void.
But, when I use A to point to a lambda that returns a String, and doesnt return void, why does it work?

Here, the lambda calls toUpperCase(), which returns String.
As lambda is providing body for the SAM of A, why does this work?



thanks.


 
Marshal
Posts: 26289
80
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're always allowed to write code which ignores the value returned by a method. For example you could write



as a statement in Java. Looks like the same applies when a lambda calls a method and ignores its return value too.
 
Enthuware Software Support
Posts: 4501
44
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You might also find a similar query that was answered here helpful.
 
Ryan Gayle
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys. I understood the significance of 'statement expressions' and void-compatible lambdas.
So, if abstractmMethod() of A is void, the lambda's body is free to return a type String, if the statement is an 'expression statement'.

Thank you for your helpful replies guys.
 
reply
    Bookmark Topic Watch Topic
  • New Topic