Java 8 Series #1 - Lambda expressions

in #programming7 years ago (edited)

Java 8 is a revolutionary release of the Java for software development platform. It includes various upgrades to the Java programming, JVM, Tools and libraries.

Java 8 provides following features for Java Programming:

  • Lambda expressions
  • Method references
  • Functional interfaces
  • Stream API
  • Default methods
  • Base64 Encode Decode
  • Static methods in interface
  • Optional class
  • Collectors class
  • ForEach() method
  • Parallel array sorting
  • Nashorn JavaScript Engine

Although Java 8 released in March 2014, today not many java developers know the good features that this release has. Even I have not gone through these topics in detail , so I decided to make a Java 8 series of topics covering all the important features that Java 8 has.

In this post I will go in detail with Java 8's best feature - Lambda expressions

In simple words ,  lambda expressions are anonymous functions, basically methods without declared names, but which can also be passed as arguments to a method as you can with an anonymous class. 

If you’re wondering where the term lambda comes from, it originates from a system developed in academia called
lambda calculus, which is used to describe computations. 

Lets take the example of the below piece of code  which  uses anonymous inner class to associate behavior with a button click 

button.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent event) {
        System.out.println("button clicked");
    }
});

Here we’re creating a new object that provides an implementation of the ActionListener class. This interface has a single method, actionPerformed, which is called by the button instance when a user actually clicks the on-screen button. The anonymous inner class provides the implementation of this method. So all it does is print out a message to say that the button has been clicked.

Anonymous inner classes were designed to make it easier for Java programmers to pass around code as data. Unfortunately, they don’t make it easy enough.  There are still four lines of boilerplate code required in order to call the single line of important logic.
Boilerplate isn’t the only issue, though: this code is fairly hard to read because it obscures the programmer’s intent. We don’t want to pass in an object; what we really want to do is pass in some behavior. In Java 8, we would write this code example as a lambda expression as

button.addActionListener(event -> System.out.println("button clicked"));

Instead of passing in an object that implements an interface, we’re passing in a block of code—a function without a name.  event is the name of a parameter, the same parameter as in the anonymous inner class example.  -> separates the parameter from the body of the lambda expression, which is just some code that is run when a user clicks our button.

So where exactly can you use lambdas? You can use a lambda expression in the context of a functional interface.  

I will cover more in detail about functional interface in my next post.

If you like my post then please comment, upvote , resteem and follow me @techroy   

Sort:  

I really enjoyed your post, thank you for sharing with us. Enjoy the vote!

Thanks @completelyanon . Do follow me for more posts on this topic.