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
javacgives the error "not a statement"; you can't use a lambda expression all by itself this way. If Eclipse is saying "The left-hand side of an assignment must be a variable", it's a misleading message, but compilers can't always be expected to give reasonable errors when given a totally confusing syntax error.