Java 8 Features Mahendra
Java 8 Features Mahendra
05/05/2025
1
Learning Objectives
After this session you will be able to understand:
Java 8 new Features.
Functional Interfaces.
Lambda Expressions.
05/05/2025
2
Java 8 New Features
Features:
Default and static methods in interfaces.
Functional interfaces.
Lambda expressions.
Java IO improvements.
05/05/2025
3
Java 8 New Features
Interface Default Method Support
Prior to Java 8, we could have only method declarations in the
interfaces
• In Java 8, we can have default methods and static methods in the
interfaces.
• Java interface default methods will help us in extending interfaces without
having the fear of breaking implementation classes.
• One of the major reason for introducing default methods in interfaces is to
enhance the Collections API in Java 8.
• default methods are implemented in Java 8 to support lambda expressions.
• Java interface default methods are also referred to as Defender Methods or
Virtual extension methods.
• For creating a default method in Java interface, we need to use “default”
keyword with the method signature.
05/05/2025
4
Java 8 New Features
Interface Static Method Support
• Java interface static method is similar to default method except that we
can’t override them in the implementation classes.
• Java interface static methods are good for providing utility methods
e.g. Checking nulls, sorting collection etc.
• We can use Java interface static methods to remove utility classes such
as Collections class. e.g. Collections.sort() method can be implemented in
interface directly (code management becomes easy)
05/05/2025
5
Java 8 New Features
interface default and static method support
Demo on static method and default method usage:
public interface INullTest { Ensuring string data
default void getDataVarified(String strData) { before use by
if (! isNull(strData )){ Checking
System.out.println("data validated:"+strData); null and blank
} string
} //Default Method
Runnable
Comparable Functional interface
Comparator
05/05/2025
8
Java 8 New Features
Using Functional Interfaces Predicate
and Lambda Expressions .
Condition 1: Find Employees who are male and age more than 20
Condition 2: All Employees who are female and age more than 18
05/05/2025
9
Java 8 New Features
Lambda Expressions.
05/05/2025
10
Java 8 New Features
Lambda Expressions.
3) Does the return type of lambda expression matches the return type of
method in interface.
interface IMessanger{
public void message(); } end lMessanger
Collections.sort(names,(s1,s2)->s2.compareTo(s1));
names.forEach(name->System.out.println(name));
After Java
8
05/05/2025
12
Thank you!!
13