
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Characteristics of Lambda Expressions in Java
The lambda expressions were introduced in Java 8 and facilitate functional programming. A lambda expression works nicely together only with functional interfaces and we cannot use lambda expressions with more than one abstract method.
Characteristics of Lambda Expression
- Optional Type Declaration − There is no need to declare the type of a parameter. The compiler inferences the same from the value of the parameter.
- Optional Parenthesis around Parameter − There is no need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
- Optional Curly Braces − There is no need to use curly braces in the 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.
Syntax
parameter -> expression body (int a, int b) -> {return a + b}
Example
@FunctionalInterface interface TutorialsPoint { void message(); } public class LambdaExpressionTest { public static void main(String args[]) { // Lambda Expression TutorialsPoint tp = () -> System.out.println("Welcome to TutorialsPoint"); tp.message(); } }
Output
Welcome to TutorialsPoint
Advertisements