Java > Java 8 Features > Lambda Expressions > Lambda Syntax
Basic Lambda Expression in Java
This snippet demonstrates the basic syntax of a lambda expression in Java, showing how to implement a functional interface.
Core Lambda Syntax
This code defines a functional interface `StringFunction` with a single abstract method `run`. A lambda expression `(str) -> new StringBuilder(str).reverse().toString()` is then used to implement this interface. The lambda takes a string as input, reverses it using `StringBuilder`, and returns the reversed string. The main method creates an instance of this lambda and applies it to the string "Hello". The result demonstrates the power of lambda expressions in providing concise implementations of functional interfaces.
interface StringFunction {
String run(String str);
}
public class LambdaExample {
public static void main(String[] args) {
StringFunction reverse = (str) -> new StringBuilder(str).reverse().toString();
System.out.println(reverse.run("Hello")); // Output: olleH
}
}
Concepts Behind the Snippet
Lambda expressions are anonymous functions that can be treated as objects. They consist of parameters, an arrow token `->`, and a body. The body can be a single expression or a block of code. The key concept is functional interfaces – interfaces with only one abstract method. Lambdas provide a shorthand notation for implementing these interfaces.
Real-Life Use Case
Lambda expressions are commonly used in event handling, callbacks, and stream processing. For example, in GUI programming, you might use a lambda to define the action to be performed when a button is clicked. In stream processing, lambdas are used extensively to filter, map, and reduce data.
Best Practices
Keep lambda expressions short and focused. If a lambda becomes too complex, consider extracting the logic into a separate named method. Use descriptive variable names and avoid unnecessary side effects. Ensure that the lambda expression's input parameters are clearly defined and understood.
Interview Tip
Be prepared to explain the syntax of a lambda expression, the concept of functional interfaces, and how lambda expressions relate to anonymous classes. Demonstrate your understanding by providing examples of how lambdas can be used to simplify code and improve readability.
When to Use Them
Use lambda expressions when you need to implement a functional interface concisely and in-line. They are particularly useful for event handling, stream processing, and any situation where a short, single-purpose function is required.
Memory Footprint
Lambda expressions generally have a smaller memory footprint compared to anonymous classes because they avoid creating a separate class file. However, the actual memory usage can vary depending on the complexity of the lambda and the specific Java implementation.
Alternatives
The main alternative to lambda expressions is anonymous classes. Before Java 8, anonymous classes were the primary way to implement functional interfaces in-line. Method references are also a related concept, allowing you to refer to an existing method as a lambda expression.
Pros
Cons
FAQ
-
What is a functional interface?
A functional interface is an interface with only one abstract method. It can have multiple default or static methods but must have exactly one abstract method. -
How do I handle exceptions in a lambda expression?
You can handle exceptions within the lambda expression's body using a try-catch block. Alternatively, you can wrap the lambda in a function that handles the exception. -
Can I use lambda expressions with any interface?
No, you can only use lambda expressions with functional interfaces, i.e., interfaces that have only one abstract method.