简体   繁体   中英

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.

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 .

I know this answer is coming long after the question was asked, but may help others wondering about the same problem as I did recently, while learning about lambdas, and because your question is valid for a novice misguided by plenty of poorly presented examples on the web.

The reason your compiler is complaining is - the example is incomplete. One way to understand the incomplete code, is that it is only the functioning portion of a context (that the example is missing) which typically can be an interface of your own design or one of the simple ones predefined in Java 8 - such as Function, BiFunction, Predicate, etc. Also, supplying the value "hello" to your String s inside the lambda {working section} somewhat beats the purpose of the s variable. Here is a more complete example of similar functionality demonstrating use of 3 String variables in lambda expression:

public class LambdaTest {
                             // define your interface
    interface Use_3_Strings {
        void apply(String s1, String s2, String s3);
    }
                             // define a test method
    public void lambdaTest1(String str1, String str2, String str3) {
                             // your lambda expression
                             // defining what to do inside { }
                             // - like a method having 3 String parameters (s1, s2, s3)
        Use_3_Strings use3strings = (s1, s2, s3) -> { System.out.println("Working with 3 strings:"); 
                                                  System.out.println("   String 1: " + s1); 
                                                  System.out.println("   String 2: " + s2);
                                                  System.out.println("   String 3: " + s3);  
                                                  StringBuilder sb = new StringBuilder();
                                                  sb.append("CSV of 3 strings:\n");
                                                  sb.append(s1);
                                                  sb.append(", ");
                                                  sb.append(s2);
                                                  sb.append(", ");
                                                  sb.append(s3);
                                                  System.out.println(sb); }; 

                             // your lambda expression in action
                             // executing what you coded inside { } above
        use3strings.apply(str1, str2, str3);

    }

}

In your main() method then do:

LambdaTest lambdaTst = new LambdaTest();
lambdaTst .lambdaTest1("Hello", "beautiful", "world");

and you should get:

Working with 3 strings:
   String 1: Hello
   String 2: beautiful
   String 3: world
CSV of 3 strings:
Hello, beautiful, world

The lambda expressions always expects a reference to be assigned.

Java 7 Code:

Valid:

new Hahaha() {
    @Override
    public void howtolaugh(int number, String sound) {
        System.out.println("say "+sound + number + " number of times");
    }
};

Java 8:

Invalid:

(int i, String sound) -> System.out.println("say "+sound + i + " number of times");

Though these two serve the same purpose, the first one is an inner class. Inner classes have a default reference to the enclosing class. When they are decompiled, a different class file is generated for them in the format <Enclosing_Class_Name>$<a_number>.class or <Enclosing_Class_Name>$<a_number>.class . Ex: TestClass$1.class.

This is not the case in lambda expressions. They do not have an implicit reference to the enclosing class neither do they have a different class generated. They need an explicit reference either as a method parameter name, or a reference object on the left hand side or a return reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM