- 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 are the rules for formal parameters in a lambda expression in Java?
A lambda expression is similar to a method that has an argument, body, and return type. It can also be called an anonymous function (method without a name).
We need to follow some rules while using formal parameters in a lambda expression.
- If the abstract method of functional interface is a zero-argument method, then the left-hand side of the arrow (->) must use empty parentheses.
- If the abstract method of functional interface is a one-argument method, then the parentheses are not mandatory.
- If the abstract method of functional interface is a multiple argument method, then the parentheses are mandatory. The formal parameters are comma-separated and can be in the same order of the corresponding functional interface.
- The mentioning type of formal parameters is not mandatory. In case we have not mentioned the type of formal parameters, then it's type can be determined by the compiler from the corresponding Target Type.
Example
interface Message { String hello(String message, String name, Gender gender); } enum Gender { MALE, FEMALE } public class LambdaFormalParameterTest { public static void main(String args[]) { Message message = (String msg, String name, Gender gender) -> { // lambda expression if(gender == Gender.MALE) { return "Hello Mr " + name + ", " + msg; } else { return "Hello Ms " + name + ", " + msg; } }; System.out.println(message.hello("Good Morning!!!", "Adithya", Gender.MALE)); System.out.println(message.hello("Good Morning!!!", "Ambica", Gender.FEMALE)); } }
Output
Hello Mr Adithya, Good Morning!!! Hello Ms Ambica, Good Morning!!!
- Related Articles
- What are the rules for a local variable in lambda expression in Java?
- What are the rules for the body of lambda expression in Java?
- What are the scoping rules for lambda expressions in Java?\n
- How many parameters can a lambda expression have in Java?
- What are the basic rules for JavaScript parameters?
- What are the identity rules for regular expression?
- What are the rules to follow while using exceptions in java lambda expressions?
- What are the rules for a functional interface in Java?
- What is the data type of a lambda expression in Java?
- Lambda expression in Java 8
- What is a target type of lambda expression in Java?
- How to write a conditional expression in 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?

Advertisements