Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our User Agreement and Privacy Policy.

Slideshare uses cookies to improve functionality and performance, and to provide you with relevant advertising. If you continue browsing the site, you agree to the use of cookies on this website. See our Privacy Policy and User Agreement for details.

4.
Introduction
In Java 8, the functional programming is the important
evolution in Object Oriented Programming world of Java.
Now, Java use lambdas, Simple Abstract Method (SAM)
etc for allow functional programming in Object Oriented
World.
There are so many new features are added in Java 8 like
Streams, Default methods etc. But Today’s we only
discuss about “Functional Programming In Java”.

5.
What is Functional Programming ?
Immutable Object: In Object Oriented and Functional
Programming, An Immutable Object is an object whose
state cannot be modified after it is created.
Functional Programming: Functional Programming
involves writing code that does not change state. One of
the key feature of Functional Programming is the First-
Class Functions.

6.
Single Abstract Method(SAM)
Interfaces have one Abstract Method are called SAM.
In Java there are lots of interfaces which only have one
abstract method(by default interface have abstract
method) like Runnable, Closeable etc.
These Single Abstract Method(SAM) Interfaces are also
called “Functional Interface”.

7.
Functional Interface
Functional Interface: In Java 8, If Functional Interface
have more than one methods, one is different and rest of
the methods have same signature of Object class
methods. These types of Interfaces also called Functional
Interfaces like Comparator etc in Java.
If interface have one abstract method and one or more
than one Default and Static methods are also called
Functional Interface.

15.
Predefined Functional Interfaces in Java 8
UnaryOperator<T>: Represents an operations on a single
operands that produces a result of the same type as its
operand.
Example:

16.
Predefined Functional Interfaces in Java 8
BinaryOperator<T, T>: Represents an operations upon
two operands of same type, producing a result of the
same type as the operands.
Example:

17.
User Defined Functional Interfaces In Java 8
IN Java 8, it is also possible to create our custom user
define Functional Interface with the help of
“@FunctionalInterface” annotation. Our Functional
Interfaces also have default methods and static methods.
@FunctionalInterface: @FunctionalInterface annotation
indicates that the type declaration is intended to be a
functional interface, as defined by Java Language
Specification.

19.
Type Inference
Type Inference is not the new topic in Java. In Java 7 we
create the Collections object with the help of Diamond
Operator(<>) like :
List<Integer> list = new ArrayList<>();
Under the hood of this code, Compiler use the “Type
Inference” technique. In this compiler pick the type
information from the left side of type declaration.

21.
Translation Of Lambda Expression
Translate the lambda expression to bytecode is major
challenge for Java, because the important thing is to
maintain backward compatibility in bytecode. There are
so many challenges were faced for conversion like:
1. How to deal with Functional Interfaces?
2. For Lambdas use Inner classes?
3. Maintain Lambda information at runtime? etc.

22.
Translation Of Lambda Expression
There are so many things are used and maintain for
lambdas in JVM, But for JVM the lambdas are not a new,
Because some languages are already use JVM for Runtime
Environment that have “Lambda Expression” like
Groovy, Scala, Clojure etc.
Today we only discuss the brief steps for translate Lambda
Expression after compilation.

24.
Translation Of Lambda Expression
Followings are the Steps:
➔ At the point at which the “Lambda Expression” would
be captured, it generates an “invokedynamic”
CallSite like :
list.forEach( s -> { System.out.println(s); } );
list.forEach( [lambda for lambda$1 as Block] );

25.
Translation Of Lambda Expression
Followings are the Steps:
➔ This CallSite is called Lambda Factory for Given
Lambda.
➔ The Dynamic Arguments to the lambda factory are the
values captured from lexical scope.
➔ The Bootstrap Method for “Lambda Factory” is called
“Lambda Metafactory”.