- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What are the rules for the body of lambda expression in Java?
A lambda expression is an anonymous function (nameless function) that has passed as an argument to another function. We need to follow some rules while using the body of a lambda expression.
Rules for the body of a lambda expression
- The body of the lambda expression can be either a single expression or more statements.
- If we are using a single expression as the body of a lambda expression, then no need to enclose the body with curly braces ({}).
- If we are using one or more statements as the body of a lambda expression, then enclosing them within curly braces({}) can be mandatory.
Syntax
(parameters) OR () -> {body with statements separated by;} OR Single Statement
Example
interface Message { String message(String name); } public class LambdaExpressionBodyTest { public static void main(String args[]) { // Lambda expression using single expressio Message msg1 = msg -> "TutorialsPoint " + msg; System.out.println(msg1.message("Lambda Expression With Expression")); // Lambda expression using statement Message msg2 = msg -> { return "TutorialsPoint " + msg; }; System.out.println(msg2.message("Lambda Expression With Statement")); // Lambda expression using multiple statements Message msg3 = msg -> { String hello = "TutorialsPoint " + msg; return hello; }; System.out.println(msg3.message("Lambda Expression With Multiple Statement")); } }
Output
TutorialsPoint Lambda Expression With Expression TutorialsPoint Lambda Expression With Statement TutorialsPoint Lambda Expression With Multiple Statement
- Related Articles
- What are the rules for a local variable in lambda expression in Java?
- What are the rules for formal parameters in a lambda expression in Java?
- What are the scoping rules for lambda expressions in Java?
- What are the identity rules for regular expression?
- What are the rules to follow while using exceptions in java lambda expressions?
- What is the data type of a lambda expression in Java?
- What are the rules for the Subscriber interface in Java 9?
- What are the rules for the Subscription interface in Java 9?
- What are the rules for the Publisher interface in Java 9?
- What are the rules for a functional interface in Java?
- What are some of the rules of creating a CSS expression?
- Lambda expression in Java 8
- What are the rules for external declarations in JShell in Java 9?
- What is a target type of lambda expression in Java?
- How to write lambda expression code for SwingUtilities.invokeLater in Java?

Advertisements