Syntax:
1. One parameter
parameter1 -> statements
2. Two parameter
(parameter1, parameter2) -> statements
3. with return keyword
(parameter1, parameter2, parameter3) -> {return ...statements}
As you can see, you can just remove the parameter data type in a lambda, since the java compiler can determine the data type from the method you are invoking from. The term used was target type. It can inferred the data type from the method that you are invoking it from.
If you used more than two parameters, you need to add a parenthesis. And if you want to use a return keyword, you need to enclose it on a braces.
=====================
public class Practice06232022 {
interface MenuOrder{
String yourOrder(String a, String b);
}
String concatenateStrings(String a, String b, MenuOrder order){
return order.yourOrder(a, b);
}
public static void main(String[] args){MenuOrder order = (menu1, menu2) -> menu1.concat(" and ").concat(menu2);
Practice06232022 practice = new Practice06232022();
System.out.println(
"You ordered: "
+ practice.concatenateStrings("tapa", "java_rice", order)
);
}
}======================
Result:
You ordered: tapa and java_rice
When it comes to shadowing of variables, the same rules were applied. Lambda will have no problem with shadowing variables. Just don't use the same name of variable in the parameter of lambda since that is not part of the scope of rules in shadowing of variables.
YOU ARE READING
Java Programming
Randomhttps://docs.oracle.com/javase/tutorial/ This is the link where I learned my java lessons online. I choose java as first programming language. I am a career shifter. I just want to start something new. I choose to write it here in Wattpad so that I...