Lambda Syntax Flashcards

1
Q

Explain why we say that a lambda expression represents a method or function as an object !

A

A lambda expression represents a method or function as an object because a lambda expression can be treated as an instance of a functional interface which is an object (the instance of the functional interface)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Give an example that shows how the lambda expression is an instance of a functional interface and run it !

A

package TEST;
public interface Multiplication{
double produit(double a,double b);
}

package TEST;
import TEST.Multiplication;

public class TEST {
public static void main(String[] args) {
/* TODO Auto-generated method stub /
Multiplication m=(a,b)-> {return a
b;};
double d=m.produit(4,5);
System.out.println(d);

} }
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Give the full syntax of a lambda expression with specifying, which parts are optional and which parts are not ! (memorized by heart because it is tricky)

A

In Java, a lambda expression is composed of a parameter list and a method body.
Theparameter list can have parentheses omitted if and only if there is exactly one parameter and its type is not explicitly specified, otherwise, parentheses are mandatory.
The arrow operator (->) separates the parameter list from the method body.
In the method body, braces can be omitted if and only if there is exactly one statement (note that returning a value counts as one statement).
If the method body contains more than one statement, braces are mandatory.
When there is only one statement in the method body and braces are omitted, the semicolon at the end of the statement can also omitted.
However, if there is only one statement in the method body and braces are used, the semicolon at the end of the statement cannot be omitted.
If the method body contains multiple statements, each statement within the braces must end with a semicolon
The closing brace must be followed by a semicolon

One last important pitfall : .In java, if the method body of a lambda expression consists of one expression between braces , you can not omit the return keyword

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Give all the cases when the parentheses as well as the braces can be omitted with examples !

A

parentheses are optional in a lambda expression only when there is exactly one parameter and its type is not explicitly specified :

EXAMPLE :
System.out.println(a -> a.startsWith(“test”));

In Java, braces are optional in a lambda expression when there is only one statement in the lambda body.

EXAMPLE :
print((a, b) -> a.startsWith(“test”));

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Give all the possible pitfalls in lambda syntax and an example for each one !

A

first pitfall of a lambda expression syntax :
If there is only one statement in the method body and the braces are not
ommitted the semicolon at the end as well as the return statement can not
be ommitted for example the following is not correct because we ommitted
the semicolon :
System.out.println(a -> { return a.startsWith(“test”) }); (let’s suppose
we have an interface that can consume a lambda expression with one string
parameter)
and the following is not correct because we ommitted the return statement :
System.out.println(a -> { a.startsWith(“test”) ;}); (let’s suppose
we have an interface that can consume a lambda expression with one string
parameter)

Second pitfallwhen we have a lambda expression whose body has multiple statements, forgetting
the semicolon after the last closing bracket is a common pitfall , the
following example illustrates the pitfall :
Multiplication m=(a,b)-> {return a*b;} (let’s suppose we have an functional interface called Multiplication that can consume a lambda with 2 double
parameters)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What variables can lambdas access and what variables it can not access !

A

The only variables that a lambda expression can access in java are local variables that are final or local variables that are effectively final or instance variables or static variables

final local variables are local variables declared as final
effectively final local variables are local variables that were initialized
only one time
and were not assigned a new value afterwards

KEY RULE
Local variables referenced from a lambda expression must be final or effectively final

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Give an example for each one with focusing on examples that don’t compile !

A

EXAMPLE 1 :
package TEST;
import TEST.Multiplication;

public class TEST {

**public static void main(String[] args)** {
	int c=0;
	c=2;
	Multiplication m=(a,b)->{System.out.println(c);return a*b;};

} }

The above example doesn’t compile because we are trying to reference
a the variable c which is not effectively final from
inside the lambda expression body which is not legal

EXAMPLE 2 :
package TEST;
import TEST.Multiplication;

public class TEST {
public static void main(String[] args) {

	Multiplication m=(a,b)->{int a=0;return 5;};

} }

The above example doesn’t compile because the local variable a is already
declared in the lambda expression parameter list and we’re trying to create
another variable with the same name in the lambda expression
body which is not legal. We can not declare 2 local variables with the same name in the same scope

EXAMPLE 3

package TEST;
import TEST.Multiplication;

public class TEST {
private int instance_variable=47;
private static int static_variable=147;

**public static void main(String[] args)** {
	int d=0;
	Multiplication m1=(a,b)->{int c=0;return 5;};
	Multiplication m2=(a,b)->{System.out.println(d);return 5;};
	Multiplication m3=(a,b)->{System.out.println(new TEST().instance_variable);return 5;};
	Multiplication m4=(a,b)->{System.out.println(static_variable);return 5;};
} }

The above example does compile successfully because, in java, we indeed can reference an instance variable, a static variable, a final local variable and an effectively final local variable from inside the lambda expression body

How well did you know this?
1
Not at all
2
3
4
5
Perfectly