• 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

Lamda in Java 8

 
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am facing some issue in compiling below code. I just wanted Lamda Expression having more than one sentence. Below is code.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

yogesh doshi wrote:I just wanted Lamda Expression having more than one sentence.


The method defined in the functional interface Calc requires a parameter and a return value, both of type int. The method defined in the functional interface Calc1 doesn't require a parameter nor a return value. So either change the type of obj1 to Calc1 or make sure the lambda expression has a parameter and returns a value, for example

Hope it helps!
Kind regards,
Roel
 
Ranch Hand
Posts: 145
4
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi yogesh,

In Simple words Lambda expression is providing implementation for functional interface abstract method. Here public interface Calc is a functional interface. Its method is doCalculation. So your providing implementation for doCalculation. When you writing lambda expression for that method( I mean providing implementation), first you need to look at method declaration. Here  doCalculation is taking one int type parameter, so you need to mention int type variable in left part of lambda expression or you can simply mention variable name. second thing , doCalculation return type is int. It may return some integer value.So you need to return some integer value in your right part of lambda expression.

Coming to your code error,


Please observe lambda expression implementation. You are not mentioned any parameter in left part of lambda expression and There is return statement at the end of right part of lambda expression. That's why you are getting compile time error.



Here you provided right implementation, So no error .


Hope this helps !


 
yogesh doshi
Ranch Hand
Posts: 49
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to your answers
 
Don't get me started about those stupid light bulbs.
    Bookmark Topic Watch Topic
  • New Topic