Forums Register Login

Lamda in Java 8

+Pie Number of slices to send: Send
I am facing some issue in compiling below code. I just wanted Lamda Expression having more than one sentence. Below is code.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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 !


+Pie Number of slices to send: Send
Thanks to your answers
And then we all jump out and yell "surprise! we got you this tiny ad!"
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 510 times.
Similar Threads
Specific Method
pass by reference???
Counting in Array List/ Standard deviation
how to work with interface in java?
What are Java reflections ?
Difference between Lambda and method reference?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:15:40.