I am trying to learn Lambda Expression in java 8. I do have installed eclipse plugin and java 8 SDK but when I am trying to execute following code eclipse is showing error.
(String s) -> {
s="hello";
System.out.println(s); }
its showing "The left-hand side of an assignment must be a variable" error.
please help.
A lambda expression (and a method reference) only makes sense in a context that expects an instance of some functional interface. It would otherwise be impossible to determine if the lambda is valid (and it would also be useless as you don't do anything with it).
Something like
Consumer<String> c = (String s) -> {
s = "hello";
System.out.println(s);
}; // as a Consumer, it doesn't really make sense for you to change s
Note that as a Consumer
, it doesn't really make sense to reassign the value of s
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With