Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Impara Lambda Syntax Variations | Getting Started with Lambda Expressions
Lambda Expressions in Java

bookLambda Syntax Variations

Lambda expressions in Java can be written in several different forms, depending on the number of parameters, whether parameter types are specified, and whether the body is a single expression or a block. Understanding these variations will help you write more concise and readable code.

When declaring a lambda, you may omit parameter types if they can be inferred from the context. For example, if a functional interface method expects a single int parameter, you can write the lambda as x -> x * x instead of (int x) -> x * x. If there is only one parameter, you may also omit the parentheses: x -> x + 1 is valid, but if there are zero or more than one parameter, you must use parentheses, such as () -> 42 or (a, b) -> a + b.

The lambda body can be a single expression or a block enclosed in curly braces. If the body is a single expression, the result is returned automatically, and you do not use the return keyword. If you use a block body, you must use the return statement to return a value, unless the method returns void. This flexibility allows you to choose the most readable style for your code.

Main.java

Main.java

copy
1234567891011121314151617181920212223242526272829303132
package com.example; import java.util.function.Supplier; import java.util.function.Function; import java.util.function.Consumer; public class Main { public static void main(String[] args) { // Lambda with no parameters Supplier<String> greet = () -> "Hello, Lambda!"; System.out.println(greet.get()); // Lambda with a single parameter, parentheses omitted Function<Integer, Integer> square = x -> x * x; System.out.println(square.apply(5)); // Lambda with a single parameter, parentheses included Function<String, String> shout = (word) -> word.toUpperCase(); System.out.println(shout.apply("lambda")); // Lambda with block body and return statement Function<Integer, String> evenOrOdd = (n) -> { if (n % 2 == 0) { return "Even"; } else { return "Odd"; } }; System.out.println(evenOrOdd.apply(7)); } }

Choosing between concise and block body syntax depends on the complexity of the logic you want to express. Concise syntax, using a single expression without curly braces or a return statement, is best for simple operations. This style makes your code shorter and easier to read when the logic is straightforward. On the other hand, if your lambda needs multiple statements, local variables, or more complex control flow, use a block body enclosed in curly braces. Remember, when using a block body for a lambda that returns a value, you must include an explicit return statement. If the lambda returns void, you do not use return unless you want to exit early.

By following these rules, you can write lambdas that are both correct and easy to maintain.

Main.java

Main.java

copy
12345678910111213141516171819
// File: Main.java package com.example; import java.util.function.Function; public class Main { public static void main(String[] args) { // Lambda with block body to compute factorial Function<Integer, Integer> factorial = (n) -> { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; }; System.out.println(factorial.apply(5)); // Output: 120 } }

1. When can you omit parentheses around the parameter list in a lambda expression?

2. When must you use a return statement in a lambda expression?

3. Rewrite the following lambda expression using a block body with an explicit return statement:

question mark

When can you omit parentheses around the parameter list in a lambda expression?

Select the correct answer

question mark

When must you use a return statement in a lambda expression?

Select the correct answer

question-icon

Rewrite the following lambda expression using a block body with an explicit return statement:

Function triple = -> { x * 3; };

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3

Chieda ad AI

expand

Chieda ad AI

ChatGPT

Chieda pure quello che desidera o provi una delle domande suggerite per iniziare la nostra conversazione

Suggested prompts:

Can you give examples of both concise and block body lambda expressions?

What are some common mistakes to avoid when writing lambda expressions in Java?

How do I decide when to use a lambda versus an anonymous class?

bookLambda Syntax Variations

Scorri per mostrare il menu

Lambda expressions in Java can be written in several different forms, depending on the number of parameters, whether parameter types are specified, and whether the body is a single expression or a block. Understanding these variations will help you write more concise and readable code.

When declaring a lambda, you may omit parameter types if they can be inferred from the context. For example, if a functional interface method expects a single int parameter, you can write the lambda as x -> x * x instead of (int x) -> x * x. If there is only one parameter, you may also omit the parentheses: x -> x + 1 is valid, but if there are zero or more than one parameter, you must use parentheses, such as () -> 42 or (a, b) -> a + b.

The lambda body can be a single expression or a block enclosed in curly braces. If the body is a single expression, the result is returned automatically, and you do not use the return keyword. If you use a block body, you must use the return statement to return a value, unless the method returns void. This flexibility allows you to choose the most readable style for your code.

Main.java

Main.java

copy
1234567891011121314151617181920212223242526272829303132
package com.example; import java.util.function.Supplier; import java.util.function.Function; import java.util.function.Consumer; public class Main { public static void main(String[] args) { // Lambda with no parameters Supplier<String> greet = () -> "Hello, Lambda!"; System.out.println(greet.get()); // Lambda with a single parameter, parentheses omitted Function<Integer, Integer> square = x -> x * x; System.out.println(square.apply(5)); // Lambda with a single parameter, parentheses included Function<String, String> shout = (word) -> word.toUpperCase(); System.out.println(shout.apply("lambda")); // Lambda with block body and return statement Function<Integer, String> evenOrOdd = (n) -> { if (n % 2 == 0) { return "Even"; } else { return "Odd"; } }; System.out.println(evenOrOdd.apply(7)); } }

Choosing between concise and block body syntax depends on the complexity of the logic you want to express. Concise syntax, using a single expression without curly braces or a return statement, is best for simple operations. This style makes your code shorter and easier to read when the logic is straightforward. On the other hand, if your lambda needs multiple statements, local variables, or more complex control flow, use a block body enclosed in curly braces. Remember, when using a block body for a lambda that returns a value, you must include an explicit return statement. If the lambda returns void, you do not use return unless you want to exit early.

By following these rules, you can write lambdas that are both correct and easy to maintain.

Main.java

Main.java

copy
12345678910111213141516171819
// File: Main.java package com.example; import java.util.function.Function; public class Main { public static void main(String[] args) { // Lambda with block body to compute factorial Function<Integer, Integer> factorial = (n) -> { int result = 1; for (int i = 2; i <= n; i++) { result *= i; } return result; }; System.out.println(factorial.apply(5)); // Output: 120 } }

1. When can you omit parentheses around the parameter list in a lambda expression?

2. When must you use a return statement in a lambda expression?

3. Rewrite the following lambda expression using a block body with an explicit return statement:

question mark

When can you omit parentheses around the parameter list in a lambda expression?

Select the correct answer

question mark

When must you use a return statement in a lambda expression?

Select the correct answer

question-icon

Rewrite the following lambda expression using a block body with an explicit return statement:

Function triple = -> { x * 3; };

Click or drag`n`drop items and fill in the blanks

Tutto è chiaro?

Come possiamo migliorarlo?

Grazie per i tuoi commenti!

Sezione 1. Capitolo 3
some-alt