Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • 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:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

ExecutorService and execute Runnable  RSS feed

 
Ranch Hand
Posts: 74
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

hi all,
I don't understand why line #3 is compiling without any problem. There is only one execute() method in ExecutorService which takes a Runnable, and Runnable run() returns void.
Line #3 shouldn't compile because toString() has an incompatible return type. But it does.
Line #4 does not compile, the compiler error is: bad return type in Lambda expression.
Thank you, Juerg
 
Saloon Keeper
Posts: 9118
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Functional interfaces that have a void return type can always be implemented by lambda expressions that have a statement as their body. Even if that statement is a method call with a return type, the return type is simply ignored.

#3 compiles because rid.toString() is a statement.
#4 does not compile because "TestRiddle" is not a statement.

For the same reason, you can do this:
And not this:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!