Introduction:
- Java's first step into functional programming
- It is an anonymous function and doesn't belong to any class
- It implements the interface methods in a concise expression clearly.
- It provides the implementation of functional interface and simplifies the software development
- First programming language uses Lambda expression is LISP, later on C, C++, C#, .Net, RUBY and etc.
Syntax:
parameter -> expression body
→ Arrow operator is introduced in Java through lambda expression that divides it into two parts(Parameter & Body).
Characteristics:
→ Optional Type Declarations
→ Optional parenthesis around parameters and curly braces
→ Optional return keyword
Interview Questions:
1. Objective of Lambda expression in JAVA:
1. Objective of Lambda expression in JAVA:
- To enable the functional programming benefits
- To write the code in more readable, maintainable and concise code
- To use API's very easily and effectively
- To enable parallel processing
2. Difference between Anonymous Inner class and Lambda Expression.
Anonymous Inner class |
Lambda Expression |
class
without name
|
function
without name
|
can
extends abstract, concrete classes and interface
|
can
extends only the functional interface (Single Abstract Method)
|
can
declare instance variable
|
can’t
declare instance variable
|
can
be instantiated
|
can’t
be instantiated
|
Inside
anonymous inner class, this always refers current anonymous inner class
object but not outer class object
|
Inside
lambda expression, this always refers Outer class object
|
highly
recommended for multiple method handling
|
highly
recommended for one abstract method in an interface i.e, functional interface
|
separate
.class file generated for every anonymous class
|
no
separate .class file generated during compilation
|
object
memory allocated in heap area
|
reside
in permanent memory of JVM (method area)
|
No comments:
Post a Comment