Unfold Java Lambda Expression

Created by Bing Image CreatorLambda functions were introduced in Java version 8.0. Java 8 was released in March 2014, so it’s been almost a decade since writing this and if you are a Java developer you might have probably used lambda expressions. Let’s…


This content originally appeared on Level Up Coding - Medium and was authored by Kanchana Ranmuthu

Created by Bing Image Creator

Lambda functions were introduced in Java version 8.0. Java 8 was released in March 2014, so it’s been almost a decade since writing this and if you are a Java developer you might have probably used lambda expressions. Let’s deep dive into Lambda expressions and see how it is derived from a very basic form of Java code.

Syntax of Lambda Expression

The syntax of a lambda expression is simple and also a bit weird.

(params) -> { body }

To understand Lambda expression in-depth, it is essential to be aware of functional interfaces.

What is a Functional Interface?

Even if the name “Functional interface” sounds to be a bit complicated material, it is not. Functional interfaces are interfaces with just one abstract method. It may contain any number of default and static methods, but only one abstract method. That’s it.

Here is an example of a Functional interface.

@FunctionalInterface
public interface FI {

void m1();

default void m2() {
//some stuff
}

static void m3() {
//some stuff
}
}

Java provides several built-in functional interfaces, such as Runnable and Comparable. The Runnable interface includes the run() method, while the Comparable interface features the compareTo() method.

Let’s now create a simple example that showcases a functional interface and its implementation class.

public interface Operations {
int doOperation(int num1, int num2);
}

public class OperationsImpl implements Operations {
@Override
public int doOperation(int num1, int num2) {
// do operation
}
}

This is the traditional way of doing this and we can implement our logic for doOperation() method. For example, if we are going to perform addition with this, it will be done as shown below.

public class OperationsImpl implements Operations{
@Override
public int doOperation(int num1, int num2) {
return num1+ num2;
}

public static void main(String[] args) {
Operations operations = new OperationsImpl();
int result = operations.doOperation(3,5);
System.out.println(result);
}
}

Also, we can do the same with an anonymous class.

public class OperationsImpl {
public static void main(String[] args) {
new Operations(){
@Override
public int doOperation(int num1, int num2) {
return num1 + num2;
}
};
}
}

As we can see, there are lot to do in these traditional approaches. It includes several steps to do. What if we can do the same but in a very simple manner? Yes, that is possible because of Lambda.

Take a moment to examine the abstract method in the functional interface. The return type, method name, and parameters are already defined there. Only implementation has to be given to the method. Also, there cannot be any other abstract methods, since it is a functional interface.

Then why do we rewrite the same stuff while utilizing the method? We can remove it and simplify the method call as below.

public class OperationsImpl {
public static void main(String[] args) {
Operations operations = (int num1, int num1) -> { return num1+ num1; };
System.out.println(operations.doOperation(3, 5));
}
}

Notice that we don’t even need to implement the interface in the OperationsImpl class.

Since the lambda expression has only one line we can omit curly braces and return keyword as well.

public class OperationsImpl {
public static void main(String[] args) {
Operations operations = (int num1, int num1) -> num1 + num1;
System.out.println(operations.doOperation(3, 5));
}
}

This approach is much simpler and cleaner compared to the traditional way. As we have seen, the lengthy overrides implemented classes can be fully dropped. Also, It has given a modernized flavor to our code!

Conclusion

Java 8’s introduction of lambda expressions marked a significant evolution in how we write and think about Java code. By enabling concise and expressive programming patterns, lambdas not only reduce boilerplate but also unlock powerful features like streams.

By mastering this feature, you will be able to write code in a functional style and reshape your coding in Java in a modern way.

Happy Coding!


Unfold Java Lambda Expression was originally published in Level Up Coding on Medium, where people are continuing the conversation by highlighting and responding to this story.


This content originally appeared on Level Up Coding - Medium and was authored by Kanchana Ranmuthu


Print Share Comment Cite Upload Translate Updates
APA

Kanchana Ranmuthu | Sciencx (2025-01-02T18:36:02+00:00) Unfold Java Lambda Expression. Retrieved from https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/

MLA
" » Unfold Java Lambda Expression." Kanchana Ranmuthu | Sciencx - Thursday January 2, 2025, https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/
HARVARD
Kanchana Ranmuthu | Sciencx Thursday January 2, 2025 » Unfold Java Lambda Expression., viewed ,<https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/>
VANCOUVER
Kanchana Ranmuthu | Sciencx - » Unfold Java Lambda Expression. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/
CHICAGO
" » Unfold Java Lambda Expression." Kanchana Ranmuthu | Sciencx - Accessed . https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/
IEEE
" » Unfold Java Lambda Expression." Kanchana Ranmuthu | Sciencx [Online]. Available: https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/. [Accessed: ]
rf:citation
» Unfold Java Lambda Expression | Kanchana Ranmuthu | Sciencx | https://www.scien.cx/2025/01/02/unfold-java-lambda-expression/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.