• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Lamda in Java 8

 
yogesh doshi
Ranch Hand
Posts: 33
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.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • 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
 
Narayana Bojja
Ranch Hand
Posts: 130
4
Hibernate Java Spring
  • 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: 33
Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to your answers
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic