Before Lambda Expressions/Functions in java
All we know that java is object oriented programming language, that means we need to create a class, with methods ( to perform actions ), whenever you want to access these methods we need to create an object of a class to invoke a method. That means accessing block of code some where is not an easy task in java because of its OOPS nature. But there is another type of programming language like Functional Programming ( which is getting more popular due to event-driven or reactive programming ). In a functional programming there is a feature to pass block of code where ever you want directly without worrying about object creation.
From java8 Functional programming concepts are added to make coding is simple and clean.
In this tutorial we are going to discuss in-deep about Lambda Functions ( called as Lambda expressions ). In an OOPS programming we can create functions outside object/class, where as in functional programming we can create a function with logic and we can create a reference to the function and then we can use that function reference as a callback function, this process generally most popularly used in javascript coding. For better understating of Lambda expressions here iam trying to give you javascript based example.
// Function Expression let sum = function(a, b) { return a + b; }; // This is called as arrow function in javascript ( which we call this type of function as Lambda function in java let sum = (a, b) => a + b; /* This arrow function is a shorter form of: let sum = function(a, b) { return a + b; }; */ alert( sum(1, 2) ); // In this line arrow function used as a parameter to alert funtion
What are characteristics/features of Lambda functions/expressions
Lambda function is a functional programming feature added to OOPS based programming language java.- A lambda expression is a block of code with parameters.
- Use a lambda expression whenever you want a block of code executed at a later point in time.
- Lambda expressions can be converted to functional interfaces.
- Lambda expressions can access effectively final variables from the enclosing scope.
- Method and constructor references refer to methods or constructors without invoking them.
- You can now add default and static methods to interfaces that provide concrete implementations.
- You must resolve any conflicts between default methods from multiple interfaces.
Lambda function syntax
(parameters) -> expression or (parameters) -> { statements; } or () -> expression
Relation between functional interface and lambda funtion
Lambda function is an implementation to functional interface. We know that functional interface is an interface which has only one abstract method.How to create functional interface
By using @FunctionalInterface annotation we can create functional interfacepackage info.code123.lambda; public class LmbdaInline { public static void main(String[] args) { // following line is a lambda function // which implemented functional interface Cal Cal cal = (a, b) -> (a + b); int result = cal.add(10, 20); System.out.println("Adition of 10 and 20 is: " + result); } } /** * Following interface Cal has only one method * so it is a functional interface, * then we can create lambda c for this * * @author Admin * */ interface Cal { int add(int a, int b); }
Multiple lines lambda expression
Now we will see an example of multiple lines lambda function, we can enclose statements with in {...} to develop multiple line lambda functions.interface Cal { int add(int a, int b); }
Cal cal = (a, b) -> { System.out.println("This is with lambda function"); return (a + b); }; int result = cal.add(10, 20); System.out.println("Addition of 10 and 20 is: " + result);In the above example we have created multiple line lambda function, and we have passed two parameters a and b, and it returns integer value, because in functional interface we have created a method which returns integer value.
Implementing functional interface with lambda function |
Anonymous interface implementation vs Lambda expression
We stated that functional interface can be implemented with lambda expression from java8, but we have regular option to implement interfaces with anonymous implementation.Anonymous implementation of interface
Following block of the code for anonymous implementation of Interface( Cal.java in our example )Cal aCal = new Cal() { @Override public int add(int a, int b) { // TODO Auto-generated method stub return a + b; } };In this anonymous implementation procedure we can create instance variables inside implementation part of the code, so anonymous implementation has a state, and after compiling a separate .class file will be created for this anonymous implementation part of the code. If we use this keyword with anonymous implementation, then this keyword represents current anonymous class object. Memory can be allocated whenever object created for anonymous class.
Lambda functions/expressions differences with anonymous implementation
Each lambda function can be converted as a private method in outer class, which is binding by invokedynamic bytecode instruction which saves time and memory by avoiding creation of separate class. The keyword this represents an object of functional interface for which we are writing lambda function. We can not create instance variables for lambda functions, but we can create local variables inside lambda functions that's why Lambda functions are stateless. We can use lambda functions to implement single abstract method interfaces which are called as functional interface.Lambda function assigned as a value to the functional interface variable
Lambda function can be passed as a method argument, or can be saved code as data
Post a Comment
Post a Comment