Java 8 New Features
java 7 – July 28th 2011
2 Years 7 Months 18 Days
Java 8 - March 18th 2014
Java 9 - September 22nd 2016
Java 10 - 2018
After Java 1.5version, Java 8 is the next major version.
Before Java 8, sun people gave importance only for objects but in 1.8version oracle people gave the
importance for functional aspects of programming to bring its benefits to Java.ie it doesn’t mean Java
is functional oriented programming language.
Java 8 New Features:
1) Lambda Expression
2) Functional Interfaces
3) Default methods
4) Predicates
5) Functions
6) Double colon operator (::)
7) Stream API
8) Date and Time API
Etc…..
1
,Java 8 New Features In Simple Way DURGASOFT
Lambda (λ) Expression
☀ Lambda calculus is a big change in mathematical world which has been introduced in 1930.
Because of benefits of Lambda calculus slowly this concepts started using in programming world.
“LISP” is the first programming which uses Lambda Expression.
☀ The other languages which uses lambda expressions are:
C#.Net
C Objective
C
C++
Python
Ruby etc.
and finally in Java also.
☀ The Main Objective of Lambda Expression is to bring benefits of functional programming into
Java.
What is Lambda Expression (λ):
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.
Ex: 1
() {
public void m1() { sop(“hello”);
sop(“hello”); }
} () { sop(“hello”); }
() sop(“hello”);
Ex:2
public void add(inta, int b) {
sop(a+b); (inta, int b) sop(a+b);
}
If the type of the parameter can be decided by compiler automatically based on the context then
we can remove types also.
The above Lambda expression we can rewrite as (a,b) sop (a+b);
2
,Java 8 New Features In Simple Way DURGASOFT
Ex: 3
public String str(String str) { (String str) return str;
return str;
}
(str) str;
Conclusions:
1) A lambda expression can have zero or more number of parameters (arguments).
Ex:
() sop(“hello”);
(int a ) sop(a);
(inta, int b) return a+b;
2) Usually we can specify type of parameter. If the compiler expects the type based on the context
then we can remove type. i.e., programmer is not required.
Ex:
(inta, int b) sop(a+b);
(a,b) sop(a+b);
3) If multiple parameters present then these parameters should be separated with comma (,).
4) If zero number of parameters available then we have to use empty parameter [ like ()].
Ex: () sop(“hello”);
5) If only one parameter is available and if the compiler can expect the type then we can remove the
type and parenthesis also.
Ex:
(int a) sop(a);
(a) sop(a);
A sop(a);
6) Similar to method body lambda expression body also can contain multiple statements. If more
than one statements present then we have to enclose inside within curly braces. If one statement
present then curly braces are optional.
7) Once we write lambda expression we can call that expression just like a method, for this
functional interfaces are required.
3
, Java 8 New Features In Simple Way DURGASOFT
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).
Ex:
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
Inside functional interface in addition to single Abstract method (SAM) we write any number of
default and static methods.
Ex:
1) interface Interf {
2) public abstract void m1();
3) default void m2() {
4) System.out.println (“hello”);
5) }
6) }
In Java 8, Sun Micro System introduced @Functional Interface annotation to specify that the interface
is Functional Interface.
Ex:
@Functional Interface
Interface Interf { This code compiles without any compilation errors.
public void m1();
}
Inside Functional Interface we can take only one abstract method, if we take more than one abstract
method then compiler raise an error message that is called we will get compilation error.
Ex:
@Functional Interface {
public void m1(); This code gives compilation error.
public void m2();
}
4