🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
In this blog post, we'll take a detailed look at the syntax of lambda expressions in Java, along with examples to illustrate the various forms they can take.
A lambda expression is an anonymous function that can have parameters and a body. The body can contain zero, one, or more lines of code. lambda expressions can be assigned to variables whose type is a functional interface – an interface with a single abstract method.
Lambda Expression Syntax
Here is the general syntax for a lambda expression in Java:
(parameters) -> expression
Or
(parameters) -> { statements; }Components of Lambda Expressions:
Parameters
The parameters section can contain zero, one, or more parameters. Their types may often be inferred by the compiler, allowing you to omit them:
Zero Parameters:
() -> System.out.println("No parameters!") One Parameter (type inferred):
s -> System.out.println(s)Multiple Parameters:
(int x, int y) -> x + y Arrow Token
The arrow token (->) separates the parameters from the body of the lambda, pointing from the parameters to the action being performed.
Body
The body of the lambda expression can be a single expression or a block of code:
Single Expression:
a -> a * a (No need for the return keyword) Code Block:
(a, b) -> { return a + b; } (Multiple statements inside braces) Lambda Expression Syntax Examples
Lambda expressions in Java can be written in various forms, depending on the signature of the functional interface's abstract method they are implementing. Here are some different ways to write lambda expressions:
1. No Parameters, No Return Value
Lambda expressions can have no parameters and no return value.Runnable noParametersNoReturnValue = () -> System.out.println("Hello, World!");2. Single Parameter, No Type Declaration
When there's only one parameter, you don't need to declare its type or use parentheses.
For example:
java.util.function.Consumer<String> singleParameterNoType = s -> System.out.println(s);3. Single Parameter, Type Declaration
You can explicitly declare the type of the parameter:
java.util.function.Consumer<String> singleParameterWithType = (String s) -> System.out.println(s);4. Multiple Parameters, Type Inferred
Java can infer the types of parameters, as shown below:
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersTypeInferred = (a, b) -> a + b;5. Multiple Parameters, Type Declared
You can declare the types of all parameters:
java.util.function.BiFunction<Integer, Integer, Integer> multipleParametersWithType = (int a, int b) -> a + b;6. Multiple Statements and Return Statement
For a lambda expression with multiple lines, you'll need curly braces and an explicit return statement:
java.util.function.BiFunction<Integer, Integer, Integer> multipleStatements = (int a, int b) -> {
int sum = a + b;
return sum;
};7. Access to Local Variables
Lambda expressions can access local variables from the enclosing scope:
int factor = 2;
java.util.function.Function<Integer, Integer> accessLocalVariable = (int a) -> a * factor;Lambda Expression Syntax - Cheat Sheet
Conclusion
Lambda expressions are a powerful feature in Java, enabling concise and readable code. By understanding the syntax and structure of lambda expressions, you can take advantage of this powerful feature in your own code.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business

Comments
Post a Comment
Leave Comment