- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
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 is the syntax for lambda expressions in Java?
A lambda expression is an anonymous method (method without a name) that can be used to provide the implementation of a method defined by the functional interface.
Syntax
([comma seperated argument-list]) -> {body}
Rules for Lambda Expression Syntax
- We can omit the datatypes since the compiler able to guess the type of parameters. When there is a single argument the parentheses also omitted.
- The arrow token (→) can be able to connect the argument and functionality. It is mandatory.
- The body contains a list of statements and expressions. In the case of a single statement or expression, the curly braces have omitted.
Example
interface EvenOrOdd { void check(int a); } public class LambdaExpressionTest1 { public static void main(String[] args) { EvenOrOdd evenOrOdd = (int a) -> { // Lambda Expression if(a% 2== 0){ System.out.println("The number "+ a +" is even"); } else { System.out.println("The number "+ a +" is odd"); } }; evenOrOdd.check(7); evenOrOdd.check(10); } }
Output
The number 7 is odd The number 10 is even
- Related Articles
- What are the scoping rules for lambda expressions in Java?\n
- What are lambda expressions in Java?
- What are block lambda expressions in Java?
- What are the characteristics of lambda expressions in Java?
- Regular Expressions syntax in Java Regex
- What are the advantages of Lambda Expressions in Java?\n
- Are lambda expressions objects in Java?
- How to debug lambda expressions in Java?
- Why we use lambda expressions in Java?
- What are lambda expressions in C#?
- What are lambda expressions and how to use them in Java?
- Differences between Lambda Expressions and Closures in Java?
- What are the rules to follow while using exceptions in java lambda expressions?
- How to implement the listeners using lambda expressions in Java?
- Lambda Expressions in C#

Advertisements