Java 8 Features
java 7 – July 28th 2011 2 Years
7 Months 18 Days Java
8 - March 18th 2014 Java(Major Version)
9 - September 22nd 2016
Java 10 – 2018
1. Lambda Expressions
2. Optional Class
3. String Joiner
4. Stream API
5. Functional Interface
6. Data and Time Changes API
7. Method Reference
8. Constructor Reference
9. Collection and Framework Changes
10. Split Iterator
LOSS FD MCCS
Lambda Expressions
The Main Objective of λ Lambda Expression
is to bring benefits of functional
programming into java.
Lambda Expression is just an
anonymous(nameless) function. That means
the function which doesn’t have the
name,return type and access modifiers.
Lambda Expression also known as
anonymous functions or closures.
Example
public void m1()
{
System.out.println(“hello”);
}
You can Write
()->
{
System.out.println(“hello”);
}
Or
()-> System.out.println(“hello”);
public void add(inta, int b)
{
System.out.println(a+b);
}
we can Write
(int a, int b) -> sop(a+b);
Or
(a,b) -> sop (a+b);
Functional Interfaces:
if an interface contain only one abstract
method, such type of interfaces are called
functional interfaces and the method is
called functional method or single abstract
method(SAM).
Example
1) Runnable It contains only run() method
2) Comparable It contains only
compareTo() method
3) ActionListener It contains only
actionPerformed()
4) Callable It contains only call()method
@FunctionalInterface
Interface Interf {
public void m1(); Allowed
}
@FunctionalInterface
Interface Interf
{
public void m1(); Not Allowed
public void m2();
}
@FunctionalInterface Not Allowed
Interface Interf
{
}
FunctionalInterface with respect to
Inheritance:
Case No:1
@FunctionalInterface
interface A {
public void methodOne();
}
@FunctionalInterface
Interface B extends A {
}
@FunctionalInterface
interface A {
public void methodOne();
}
@FunctionalInterface
interface B extends A {
public void methodOne();
}
@FunctionalInterface {
interface A
{
public void methodOne();
}
@FunctionalInterface
interface B extends A {
public void methodTwo();
}
Where we use Functional
Interface
Once we write Lambda expressions to invoke it’s
functionality, then FunctionalInterface is required. We
can use FunctionalInterface reference to refer Lambda
Expression. Where ever FunctionalInterface concept is
applicable there we can use Lambda Expressions
Without lambda Functional
Interface
interface Interf {
public void methodOne()
{
}
public class Demo implements Interface
{
public void methodOne() {
System.out.println( method one execution );
}
public class Test {
public static void main(String[] args) {
Interfi = new Demo();
i.methodOne();
}
}
interface Interf {
public void methodOne()
{
}
class Test {
public static void main(String[] args) {
Interfi = ()
System.out.println(“MethodOne Execution”);
i.methodOne();
}
}
Example:2
Interface Interf {
public void sum(inta,int b);
}
class Demo implements Interf {
public void sum(inta,int b) {
System.out.println(“The sum:” +(a+b));
}
}
public class Test {
public static void main(String[] args) {
Interfi = new Demo();
i.sum(20,5);
}
}
Examples
Anonymous class
Lambda Expression
Example 2
We can also write
Example3 Runnable
Interface
Example 3
String Joinners
Stream API
Pre define functions
interface
Consumer functional
interface