Over a million developers have joined DZone.

First Sight With Java 8 Lambda Expressions with Examples

DZone's Guide to

First Sight With Java 8 Lambda Expressions with Examples

· Java Zone ·
Free Resource

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.

The intent of this Online Tutorial on Java is to give a high level overview of the upcoming  Lambda Project, which is being developed upon  JSR-335 ( Second Early Draft Review of JSR-335 is closed in June, 2012). The  Lambda Project is going to be a part of Java SE 8 ( Java 1.8), which is set to be released in 2013.

The JSR-335 introduces  Closures in Java. The  Closures are quite common in most of the popular languages like  C++ and C #.  Closures let us create function pointers and pass them as parameters; also the methods will be able to enclose the surrounding context as a snapshot so that it can be passed along with the method. In this article we will go through the Java 8 features, and introduce ourselves with the Lambda Expressions. I have tried to put some sample example programs codes to explain the concept and the syntax, better.

Java Programming Language provides us with the concepts of Interfaces that are capable of defining abstract methods only. Interfaces define an API and they expect users or vendors to provide implementations for their methods.
Many a times, we do not create separate implementation classes for a given interface. Instead of that we write inline interface implementations, which are also called as  Anonymous Inner Classes.
Anonymous classes are used widely. In our day-to-day life we can see their presence in number of libraries.  Anonymous classes are majorly used to specify event handlers within the components that generate events. Second major use of  Anonymous classes can be seen in  multithreaded programs. Many a times, instead of creating dedicated  Runnable/Callable implementation, we write  Anonymous classes.
As we discussed, an  Anonymous class is nothing but an inline implementation of a given interface. Usually, we pass such implementation classes as an argument to a method and then the method will internally invoke methods on the passed implementation class. Hence such interfaces are called as  Callback interfaces and their methods are called as  Callback methods.
Though the  Anonymous classes are being used everywhere, they have number of problems. First and major issue is with the complexity. These classes make the code bulky and complex. Sometimes, it is also called as a  Vertical Problem, because these classes will increase the vertical length of a program. Secondly, they cannot access the non-final members of the enclosing class. They sound confusing in terms of shadow variables and with the behavior the ‘ this’ keyword. If an  Anonymous class has variable with a name same as that of any member of the enclosing class then the inner variable will shadow the outer member variable. In that case the outer member will be invisible to the  anonymous class and it can't even be accessed by mean of the ' this' keyword. The ‘ this’ keyword in  Anonymous class points to itself and not to the enclosing class.

public void anonymousExample() {
String nonFinalVariable = "Non Final Example";
String variable = "Outer Method Variable";
new Thread(new Runnable() {
String variable = "Runnable Class Member";
public void run() {
String variable = "Run Method Variable";
//Below line gives compilation error.
//System.out.println("->" + nonFinalVariable);
System.out.println("->" + variable);
System.out.println("->" + this.variable);

This post originally appeared at ‘At First Sight’ With Closures in Java.

How do you break a Monolith into Microservices at Scale? This ebook shows strategies and techniques for building scalable and resilient microservices.


Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}