Java 8 Lambda Expressions

The Java programming language introduced in 1996 by Sun Microsystems as a cross platform environment and an object oriented programming structure has undergone tremendous changes.  From introducing inner classes, JDBC, RMI, Annotations, the latest addition to the Java world is the entry of Lambda expressions. This post will provide an overview of ‘Lambda expressions’ and why it is necessary.

Missing feature of Java:

Java the strictly object oriented programming language makes one think only in terms of objects and classes. Given that time has evolved and more languages have come into existence with a different set of features – the creators and modifiers of Java felt the need to fill certain gaps and keep it updated with the current times. The ‘functional programming’ aspect of other languages was the compelling feature that was loudly missing in the Java programming world. As an example Javascript is a functional and object oriented programming language.

Features of Functional programming language:

The most important feature of a functional programming language is the “closure” property. The “closure” property enables a function to access the variables in its function as well as the variables in the “parent scope” as well as all the global variables. Let us see an illustration of this in Javascript:

<html>
 <head> </head>
 <body>
 <script>
 function numbers(num1, num2){
 alert("Javascript closure example");
 //Inner function
 function show(){
 num1=num1+1;
 alert(num1+num2);
 }
 show();
 }
 numbers(1,2);
 </script>
 </body>
 </html>

In the above code, the inner function “show” has access to the variables within it as well as the variables in the “parent function” as well.

Lambda expressions:

With the introduction to functional programming and closure property, we will now move to the real crux of this post – Lambda expressions.

A Lambda expression is almost like an anonymous class – but it isn’t. Lambda expressions also do not have a name, access modifier or return type. It is also implemented in the same place it is declared. It is also good for methods that are going to be used only once.

Syntax of Lambda expressions:

(arguments) -> {body}

  1. There can be zero, one or more parameters
  2. If there are zero parameters, they have to be represented by empty braces(like ‘()’)
  3. If the body has a single statement – it need not be enclosed in braces
  4. However, if the body has more than statement, then it has to be enclosed in curly braces.

Let us have a look at the following example of Lambda expressions:

 //Program to illustrate Lambda expressions
package javaapplication1;
public class JavaApplication1 {
    public static void simple(String s1){
        int i=0;
        while(i<5){
         // Lambda expression
        Runnable r=() -> System.out.println("Hello  " +s1 );   
        new Thread(r).start();
        i++;
    }  
    }
    public static void main(String [] args) {
           simple("Whizlabs");
    }      
}

We are using the functional interface ‘Runnable’ to illustrate the working of our Lambda expression. The Lambda expression is used to start a thread and print the message “Hello Whizlabs” five times. Note that, the variable is in the parent scope and it is easily available in our Lambda expression.

This will be the output of our program:

run:
Hello  Whizlabs
Hello  Whizlabs
Hello  Whizlabs
Hello  Whizlabs
Hello  Whizlabs
BUILD SUCCESSFUL (total time: 1 second)

Note that a functional interface is an interface which has only one abstract method defined in it.

We saw Lambda expressions and functional programming in this post. We will explore more Java related topics in future posts.

Preparing for OCPJP 8 Certification? Pass in 1st attempt through Whizlabs OCPJP 8 Practice Test !  Start with Free Trial!


About Aditi Malhotra

Aditi Malhotra is the Content Marketing Manager at Whizlabs. Having a Master in Journalism and Mass Communication, she helps businesses stop playing around with Content Marketing and start seeing tangible ROI. A writer by day and a reader by night, she is a fine blend of both reality and fantasy. Apart from her professional commitments, she is also endearing to publish a book authored by her very soon.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top