0% found this document useful (0 votes)
48 views

JAVA 8 - Lambda Expression

Lambda expressions in Java 8 enable functionality to be passed as arguments and treated as code, avoiding the need for anonymous classes in some cases. A lambda expression has a parameter section before "->" and an expression body. Lambda expressions allow referring to effectively final variables in their scope. Examples demonstrate using lambda expressions to define functional interfaces for mathematical operations and greetings.

Uploaded by

Sonal Mhatre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
48 views

JAVA 8 - Lambda Expression

Lambda expressions in Java 8 enable functionality to be passed as arguments and treated as code, avoiding the need for anonymous classes in some cases. A lambda expression has a parameter section before "->" and an expression body. Lambda expressions allow referring to effectively final variables in their scope. Examples demonstrate using lambda expressions to define functional interfaces for mathematical operations and greetings.

Uploaded by

Sonal Mhatre
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

JAVA 8 – LAMBDA

EXPRESSIONS

pg. 1
Why Java needs Lambda?
One issue with anonymous classes is that if the implementation of your anonymous class is very
simple, such as an interface that contains only one method, then the syntax of anonymous classes
may seem unwieldy and unclear. In these cases, you're usually trying to pass functionality as an
argument to another method, such as what action should be taken when someone clicks a
button. Lambda expressions enable you to do this, to treat functionality as method argument, or
code as data.

Anonymous Class

1. Enable you to declare and instantiate a class at the same time


2. They are like local classes except that they do not have a name.
3. Use them if you need to use a local class only once.

Anonymous Class

HelloWorld Greeting = new HelloWorld() {


String name = "tout le monde";
public void greet() {
greetSomeone("abc");
}
public void greetSomeone(String someone) {
name = someone;
System.out.println("Salut " + name);
}
};

Syntax
A lambda expression is characterized by the following syntax −
parameter -> expression body

Following are the important characteristics of a lambda expression −


Optional type declaration − No need to declare the type of a parameter. The compiler can
inference the same from the value of the parameter.
Optional parenthesis around parameter − No need to declare a single parameter in parenthesis.
For multiple parameters, parentheses are required.
Optional curly braces − No need to use curly braces in expression body if the body contains a
single statement.
Optional return keyword −The compiler automatically returns the value if the body has a single
expression to return the value. Curly braces are required to indicate that expression returns a
value.

pg. 2
Surface Advantage of Lambdas
• Old
button.addActionListener(new ActionListener() {
@Override public void actionPerformed(ActionEvent e) { doSomethingWith(e);
}
});

• New
button.addActionListener(e -> doSomethingWith(e));

Lambda Expressions Example


Java8Tester.java

public class Java8Tester {


public static void m ain(String args[]){
Java8Tester tester = new Java8Tester();
//with type declaration
MathOperation addition = (int a, int b) -> a + b;
//with out type declaration
MathOperation subtraction = (a, b) -> a - b;
//with return statem ent along with curly braces
MathOperation m ultiplication = (int a, int b) -> { return a * b; };
//without return statem ent and without curly braces
MathOperation division = (int a, int b) -> a / b;
System .out.println("10 + 5 = " + tester.operate(10, 5, addition));
System .out.println("10 - 5 = " + tester.operate(10, 5, subtraction));
System .out.println("10 x 5 = " + tester.operate(10, 5, m ultiplication));
System .out.println("10 / 5 = " + tester.operate(10, 5, division));
//with parenthesis
GreetingService greetService1 = m essage ->
System .out.println("Hello " + m essage);
//without parenthesis
GreetingService greetService2 = (m essage) ->
System .out.println("Hello " + m essage);
greetService1.sayMessage("ABC");
greetService2.sayMessage("XYZ");
}
interface MathOperation {
int operation(int a, int b);
}
interface GreetingService {
void sayMessage(String m essage);
}
private int operate(int a, int b, MathOperation m athOperation){

pg. 3
return m athOperation.operation(a, b);
}
}

Verify the Result


It should produce the following output −
10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello ABC
Hello XYZ

Following are the important points to be considered in the above example.


Lambda expressions are used primarily to define inline implementation of a functional interface,
i.e., an interface with a single method only. In the above example, we've used various types of
lambda expressions to define the operation method of MathOperation interface. Then we have
defined the implementation of sayMessage of GreetingService. Lambda expression eliminates
the need of anonymous class and gives a very simple yet powerful functional programming
capability to Java.

Scope
Using lambda expression, you can refer to final variable or effectively final variable which is
assigned only once. Lambda expression throws a compilation error, if a variable is assigned a
value the second time.

Scope Example
Java8Tester.java
public class Java8Tester {
final static String salutation = "Hello! ";
public static void m ain(String args[]){
GreetingService greetService1 = m essage ->
System .out.println(salutation + m essage);
greetService1.sayMessage("ABC");
}
interface GreetingService {
void sayMessage(String m essage);
}
}

Verify the Result


It should produce the following output −
Hello! ABC

pg. 4

You might also like