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}
- There can be zero, one or more parameters
- If there are zero parameters, they have to be represented by empty braces(like ‘()’)
- If the body has a single statement – it need not be enclosed in braces
- 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!
- Top 45 Fresher Java Interview Questions - March 9, 2023
- 25 Free Practice Questions – GCP Certified Professional Cloud Architect - December 3, 2021
- 30 Free Questions – Google Cloud Certified Digital Leader Certification Exam - November 24, 2021
- 4 Types of Google Cloud Support Options for You - November 23, 2021
- APACHE STORM (2.2.0) – A Complete Guide - November 22, 2021
- Data Mining Vs Big Data – Find out the Best Differences - November 18, 2021
- Understanding MapReduce in Hadoop – Know how to get started - November 15, 2021
- What is Data Visualization? - October 22, 2021