Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lambda Expression example not working in java 8

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.

like image 391
DCoder Avatar asked Apr 14 '14 19:04

DCoder


1 Answers

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.

like image 99
Sotirios Delimanolis Avatar answered Oct 09 '22 10:10

Sotirios Delimanolis