SlideShare a Scribd company logo
2
Most read
8
Most read
10
Most read
Java 8 Features
Elias Hasnat
http://github.com/claymodel
Java 8 Features
1. Default and Static methods in Interface
2. Lambda Expressions
3. Optional
4. Streams
5. Method References
6. Data Time API
7. Nashorn Javascript Engine
8. Parallel Arrays
1. Interface Method (Default & Static)
=> Java 8 introduces new features to interfaces.
=> Before java 8 interface having only abstract methods but
now java 8 added two more type of methods to interface.
a. First one is default method. A method which is having a default keyword
with method body. Actually interfaces wont have any implemented
methods but now with java 8 default method we can add a method with
default implementation by using "default " keyword. The classes which are
implementing this interface can use these default method and same time it
can override the existing method. But its not mandatory to override.
1. Interface Method (Default & Static)
interface DefaultInterface{
abstract void add();
default void display(){
System.out.println("Interface
Default Method");
}
}
1. Interface Method (Default & Static)
b.The second new method introduced in java 8 is static method. Yes like in
classes now we can define a static methods inside interface by using "static".
Basically static methods which are defined in interface are interface level only.
if we want to call these static methods which are defined in interfaces we need
to use interface name so that we can access these methods.
1. Interface Method (Default & Static)
interface StaticInterface{
abstract void add();
default void display(){
System.out.println("Default Method");
}
public static void show(){
System.out.println("Static Method");
}
}
2. Lambda Expressions
=> One of the most awaited and biggest
release in java 8 is lamda expressions.
=> Ability to pass functionality/ behavior to
methods as arguments.
=> Allows us to write a method in the same
place we are going to use it.
2. Lambda Expressions
interface LambdaExpression{
public static void main(String[] args){
Arrays.asList( "j", "a", "v" ,"a","8").
forEach( e -> System.out.print( e ) );
}
}
3. Optional java.util.Optional
=> One of the best and cool feature of java 8 is Optional
class. Which is a final calls from java.util package.
=> The major repeating statement in every project is
checking "NullPointerException". Before using any object
we need to check whether it is null or not if its not null then
only we need to proceed.
=> Optional is just like a container which holds a value of
type <T> or "null". By using isPresent() method of Optional
class we can check particular object is null not not.
3. Optional java.util.Optional
class OptionalSample{
public static void main(String[] args ){
Optional< String > str = Optional.ofNullable(
null );
System.out.println( "str having value ? " +
str.isPresent() ); // output : str having value ? false
}
}
4. Streams
=> One of the excellent feature from java 8 as java.util.stream.
=> Stream API introduces real-world functional-style
programming into the Java.
=> Provides functional operations on stream of elements such
as list , set and map
=> Supports filtering, mapping and removal of duplicates of
elements in collections, are implemented lazily.
=> Now we can get Streams from collections, arrays and
bufferedReaders etc.
4. Streams
class StreamSample{
public static void main(String[] args ){
Arrays.stream(new int[] {1, 2, 3,4,5})
.map(n -> 2 * n + 1)
.average()
.ifPresent(System.out::println); // output: 7.0
}
}
5. Method Reference
=> We can use lambda expressions to create
anonymous methods.
=> Sometimes, however, a lambda expression does
nothing but call an existing method. In those cases, it's
often clearer to refer to the existing method by name.
=> Using Method references refer to the existing method
by name, they are compact, easy-to-read lambda
expressions for methods that already have a name
5. Method Reference
class MethodRefSample{
public void show(String str){
System.out.println(str);
}
public static void main(String[] args ){
Arrays.asList("a", "b", "c").forEach(new A()::
show); // a b c
}
}
6. Data Time API
=> The next cool feature from java 8 is new
date time API(jsr 310) added within java.time
package.
=> Before java 8 if we want to format dates we
use SimpleDateFormatter class in java 8 while
declaring date itself it has constructor to pass
format of date.
6. Data Time API
=> Some of the new classes introduced in java 8 date time
are as follows.
1. LocalTime
2. LocalDate
3. LocalDateTime
4. OffsetDate
6. OffsetTime
7. OffsetDateTime
6. Data Time API
class DateTimeAPISample{
public static void main(String[] args ){
LocalDate currentDate = LocalDate.now();
System.out.println(currentDate);
LocalDate twentyMarch2015 = LocalDate.of(2015, Month.
MARCH, 06);
System.out.println(twentyMarch2015); //2015-03-06
LocalDate firstApril2015 = LocalDate.of(2015, 4, 1);
System.out.println(firstApril2015);//2015-04-01
}
7. Nashorn Javascript Engine
=> Java 8 come with new Nashorn Javascript
Engine which is allowing us to develop and run
JavaScript applications.
7. Nashorn Javascript Engine
class JavaScriptSample{
public static void main(String[] args ){
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName( "JavaScript"
);
System.out.println( engine.getClass().getName() );
System.out.println( "output:" + engine.eval( "function show() {
return 10; }; show();" ) );
}
}
jdk.nashorn.api.scripting.NashornScriptEngine
output:10
8. Parallel Array Sorting
=> As of now java 7 we already having Arrays.sort()
method to sort objects now java 8 introduced parallel
sorting which has more speed than arrays.sort() and
follows Fork/Join framework introduced in Java 7 to assign
the sorting tasks to multiple threads that are available in the
thread pool.
=> Java 8 added parallel sorting functionalities to java.util.
Arrays to take advantage of multithread machines
8. Parallel Array Sorting
class ParallelArray{
public static void main(String[] args ){
int arr[]={1,4,2,8,5};
Arrays.parallelSort(arr);
for(int i:arr){
System.out.println(i);
}
}
}
Thank You

More Related Content

What's hot (20)

PDF
Java 8 Lambda Expressions & Streams
NewCircle Training
 
ODP
Introduction to Java 8
Knoldus Inc.
 
PPTX
Maven Basics - Explained
Smita Prasad
 
PDF
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
shaunthomas999
 
PDF
Java 8 Stream API. A different way to process collections.
David Gómez García
 
PDF
Java 9 New Features
Ali BAKAN
 
PDF
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
PPTX
Asynchronous programming in C#
Bohdan Pashkovskyi
 
PDF
Introduction to gradle
NexThoughts Technologies
 
PPT
Java collections concept
kumar gaurav
 
PPTX
This keyword in java
Hitesh Kumar
 
PPT
Generics in java
suraj pandey
 
PDF
Java 10 New Features
Ali BAKAN
 
ODP
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
PDF
REST APIs with Spring
Joshua Long
 
PPTX
TestNG Framework
Levon Apreyan
 
PDF
Retrofit
Amin Cheloh
 
PPTX
Regular Expressions in Java
OblivionWalker
 
PDF
Spring Boot
HongSeong Jeon
 
PPTX
React hooks
Ramy ElBasyouni
 
Java 8 Lambda Expressions & Streams
NewCircle Training
 
Introduction to Java 8
Knoldus Inc.
 
Maven Basics - Explained
Smita Prasad
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
shaunthomas999
 
Java 8 Stream API. A different way to process collections.
David Gómez García
 
Java 9 New Features
Ali BAKAN
 
Basics of React Hooks.pptx.pdf
Knoldus Inc.
 
Asynchronous programming in C#
Bohdan Pashkovskyi
 
Introduction to gradle
NexThoughts Technologies
 
Java collections concept
kumar gaurav
 
This keyword in java
Hitesh Kumar
 
Generics in java
suraj pandey
 
Java 10 New Features
Ali BAKAN
 
Routing & Navigating Pages in Angular 2
Knoldus Inc.
 
REST APIs with Spring
Joshua Long
 
TestNG Framework
Levon Apreyan
 
Retrofit
Amin Cheloh
 
Regular Expressions in Java
OblivionWalker
 
Spring Boot
HongSeong Jeon
 
React hooks
Ramy ElBasyouni
 

Similar to Java8 features (20)

PPTX
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
Srimanta Sahu
 
PPTX
New Features of JAVA SE8
Dinesh Pathak
 
PDF
Java se 8 new features
Aymen Masri
 
PPTX
Java 8 - An Overview
Indrajit Das
 
PPTX
Java 8 Features
Trung Nguyen
 
DOCX
Colloquium Report
Mohammad Faizan
 
PPTX
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
PPTX
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
PDF
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Scholarhat
 
PDF
2014 10 java 8 major new language features
Neil Brown
 
PPTX
New Features in JDK 8
Martin Toshev
 
PPTX
java8
Arik Abulafya
 
ODP
Hello Java 8
Adam Davis
 
PPTX
New features in jdk8 iti
Ahmed mar3y
 
PPT
whats new in java 8
Dori Waldman
 
PPTX
Java 8 Features
Leninkumar Koppoju
 
PPTX
Intro to java 8
John Godoi
 
PDF
Java8
Felipe Mamud
 
PDF
Java 8
Sheeban Singaram
 
PPTX
Java SE 8 - New Features
Naveen Hegde
 
A-Brief-Introduction-To-JAVA8_By_Srimanta_Sahu
Srimanta Sahu
 
New Features of JAVA SE8
Dinesh Pathak
 
Java se 8 new features
Aymen Masri
 
Java 8 - An Overview
Indrajit Das
 
Java 8 Features
Trung Nguyen
 
Colloquium Report
Mohammad Faizan
 
Introduction of Java 8 with emphasis on Lambda Expressions and Streams
Emiel Paasschens
 
Java 8 Intro - Core Features
GlobalLogic Ukraine
 
Java 8 Interview Questions and Answers PDF By ScholarHat.pdf
Scholarhat
 
2014 10 java 8 major new language features
Neil Brown
 
New Features in JDK 8
Martin Toshev
 
Hello Java 8
Adam Davis
 
New features in jdk8 iti
Ahmed mar3y
 
whats new in java 8
Dori Waldman
 
Java 8 Features
Leninkumar Koppoju
 
Intro to java 8
John Godoi
 
Java SE 8 - New Features
Naveen Hegde
 
Ad

More from Elias Hasnat (20)

PDF
BLE.pdf
Elias Hasnat
 
PDF
FacialRecognition-May-8-2020.pdf
Elias Hasnat
 
PDF
Smart City IoT Solution Improved
Elias Hasnat
 
PDF
Connected vehicle mobility as a service (maas)
Elias Hasnat
 
PDF
Lorawan for agriculture, haccp hazard analysis and critical control point
Elias Hasnat
 
PDF
IoT Security with Azure
Elias Hasnat
 
PDF
産業向け AWS IoT ソリューション
Elias Hasnat
 
PDF
Soap vs REST-API
Elias Hasnat
 
PDF
AIIoT組み込みシステム向けIEEE1888通信スタック
Elias Hasnat
 
PDF
IoT security reference architecture
Elias Hasnat
 
PDF
Intelligent video stream detection platform
Elias Hasnat
 
PDF
Machine Learning Algorithms
Elias Hasnat
 
PDF
REST API
Elias Hasnat
 
PDF
Mqtt
Elias Hasnat
 
PDF
Reinforcement learning
Elias Hasnat
 
PDF
Dalvikよりart
Elias Hasnat
 
PDF
K means
Elias Hasnat
 
PDF
Unity sdk-plugin
Elias Hasnat
 
PDF
Cocos2dx
Elias Hasnat
 
PDF
China Mobile Market
Elias Hasnat
 
BLE.pdf
Elias Hasnat
 
FacialRecognition-May-8-2020.pdf
Elias Hasnat
 
Smart City IoT Solution Improved
Elias Hasnat
 
Connected vehicle mobility as a service (maas)
Elias Hasnat
 
Lorawan for agriculture, haccp hazard analysis and critical control point
Elias Hasnat
 
IoT Security with Azure
Elias Hasnat
 
産業向け AWS IoT ソリューション
Elias Hasnat
 
Soap vs REST-API
Elias Hasnat
 
AIIoT組み込みシステム向けIEEE1888通信スタック
Elias Hasnat
 
IoT security reference architecture
Elias Hasnat
 
Intelligent video stream detection platform
Elias Hasnat
 
Machine Learning Algorithms
Elias Hasnat
 
REST API
Elias Hasnat
 
Reinforcement learning
Elias Hasnat
 
Dalvikよりart
Elias Hasnat
 
K means
Elias Hasnat
 
Unity sdk-plugin
Elias Hasnat
 
Cocos2dx
Elias Hasnat
 
China Mobile Market
Elias Hasnat
 
Ad

Recently uploaded (20)

PPTX
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
PPTX
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
PDF
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
PDF
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
PPTX
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
PDF
The Internet - By the numbers, presented at npNOG 11
APNIC
 
PPTX
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
PDF
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
PPTX
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PPTX
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
PDF
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
PDF
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
PDF
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
PDF
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
PPTX
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
PPTX
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
PDF
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
PPTX
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
DOCX
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
PPTX
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 
法国巴黎第二大学本科毕业证{Paris 2学费发票Paris 2成绩单}办理方法
Taqyea
 
Metaphysics_Presentation_With_Visuals.pptx
erikjohnsales1
 
Enhancing Parental Roles in Protecting Children from Online Sexual Exploitati...
ICT Frame Magazine Pvt. Ltd.
 
Cleaning up your RPKI invalids, presented at PacNOG 35
APNIC
 
Softuni - Psychology of entrepreneurship
Kalin Karakehayov
 
The Internet - By the numbers, presented at npNOG 11
APNIC
 
04 Output 1 Instruments & Tools (3).pptx
GEDYIONGebre
 
BRKACI-1001 - Your First 7 Days of ACI.pdf
fcesargonca
 
Presentation3gsgsgsgsdfgadgsfgfgsfgagsfgsfgzfdgsdgs.pptx
SUB03
 
PHIPA-Compliant Web Hosting in Toronto: What Healthcare Providers Must Know
steve198109
 
BRKAPP-1102 - Proactive Network and Application Monitoring.pdf
fcesargonca
 
Digital burnout toolkit for youth workers and teachers
asociatiastart123
 
FutureCon Seattle 2025 Presentation Slides - You Had One Job
Suzanne Aldrich
 
Boardroom AI: The Next 10 Moves | Cerebraix Talent Tech
ssuser73bdb11
 
Networking_Essentials_version_3.0_-_Module_3.pptx
ryan622010
 
Networking_Essentials_version_3.0_-_Module_5.pptx
ryan622010
 
Top 10 Testing Procedures to Ensure Your Magento to Shopify Migration Success...
CartCoders
 
L1A Season 1 ENGLISH made by A hegy fixed
toszolder91
 
Custom vs. Off-the-Shelf Banking Software
KristenCarter35
 
Lec15_Mutability Immutability-converted.pptx
khanjahanzaib1
 

Java8 features

  • 1. Java 8 Features Elias Hasnat http://github.com/claymodel
  • 2. Java 8 Features 1. Default and Static methods in Interface 2. Lambda Expressions 3. Optional 4. Streams 5. Method References 6. Data Time API 7. Nashorn Javascript Engine 8. Parallel Arrays
  • 3. 1. Interface Method (Default & Static) => Java 8 introduces new features to interfaces. => Before java 8 interface having only abstract methods but now java 8 added two more type of methods to interface. a. First one is default method. A method which is having a default keyword with method body. Actually interfaces wont have any implemented methods but now with java 8 default method we can add a method with default implementation by using "default " keyword. The classes which are implementing this interface can use these default method and same time it can override the existing method. But its not mandatory to override.
  • 4. 1. Interface Method (Default & Static) interface DefaultInterface{ abstract void add(); default void display(){ System.out.println("Interface Default Method"); } }
  • 5. 1. Interface Method (Default & Static) b.The second new method introduced in java 8 is static method. Yes like in classes now we can define a static methods inside interface by using "static". Basically static methods which are defined in interface are interface level only. if we want to call these static methods which are defined in interfaces we need to use interface name so that we can access these methods.
  • 6. 1. Interface Method (Default & Static) interface StaticInterface{ abstract void add(); default void display(){ System.out.println("Default Method"); } public static void show(){ System.out.println("Static Method"); } }
  • 7. 2. Lambda Expressions => One of the most awaited and biggest release in java 8 is lamda expressions. => Ability to pass functionality/ behavior to methods as arguments. => Allows us to write a method in the same place we are going to use it.
  • 8. 2. Lambda Expressions interface LambdaExpression{ public static void main(String[] args){ Arrays.asList( "j", "a", "v" ,"a","8"). forEach( e -> System.out.print( e ) ); } }
  • 9. 3. Optional java.util.Optional => One of the best and cool feature of java 8 is Optional class. Which is a final calls from java.util package. => The major repeating statement in every project is checking "NullPointerException". Before using any object we need to check whether it is null or not if its not null then only we need to proceed. => Optional is just like a container which holds a value of type <T> or "null". By using isPresent() method of Optional class we can check particular object is null not not.
  • 10. 3. Optional java.util.Optional class OptionalSample{ public static void main(String[] args ){ Optional< String > str = Optional.ofNullable( null ); System.out.println( "str having value ? " + str.isPresent() ); // output : str having value ? false } }
  • 11. 4. Streams => One of the excellent feature from java 8 as java.util.stream. => Stream API introduces real-world functional-style programming into the Java. => Provides functional operations on stream of elements such as list , set and map => Supports filtering, mapping and removal of duplicates of elements in collections, are implemented lazily. => Now we can get Streams from collections, arrays and bufferedReaders etc.
  • 12. 4. Streams class StreamSample{ public static void main(String[] args ){ Arrays.stream(new int[] {1, 2, 3,4,5}) .map(n -> 2 * n + 1) .average() .ifPresent(System.out::println); // output: 7.0 } }
  • 13. 5. Method Reference => We can use lambda expressions to create anonymous methods. => Sometimes, however, a lambda expression does nothing but call an existing method. In those cases, it's often clearer to refer to the existing method by name. => Using Method references refer to the existing method by name, they are compact, easy-to-read lambda expressions for methods that already have a name
  • 14. 5. Method Reference class MethodRefSample{ public void show(String str){ System.out.println(str); } public static void main(String[] args ){ Arrays.asList("a", "b", "c").forEach(new A():: show); // a b c } }
  • 15. 6. Data Time API => The next cool feature from java 8 is new date time API(jsr 310) added within java.time package. => Before java 8 if we want to format dates we use SimpleDateFormatter class in java 8 while declaring date itself it has constructor to pass format of date.
  • 16. 6. Data Time API => Some of the new classes introduced in java 8 date time are as follows. 1. LocalTime 2. LocalDate 3. LocalDateTime 4. OffsetDate 6. OffsetTime 7. OffsetDateTime
  • 17. 6. Data Time API class DateTimeAPISample{ public static void main(String[] args ){ LocalDate currentDate = LocalDate.now(); System.out.println(currentDate); LocalDate twentyMarch2015 = LocalDate.of(2015, Month. MARCH, 06); System.out.println(twentyMarch2015); //2015-03-06 LocalDate firstApril2015 = LocalDate.of(2015, 4, 1); System.out.println(firstApril2015);//2015-04-01 }
  • 18. 7. Nashorn Javascript Engine => Java 8 come with new Nashorn Javascript Engine which is allowing us to develop and run JavaScript applications.
  • 19. 7. Nashorn Javascript Engine class JavaScriptSample{ public static void main(String[] args ){ ScriptEngineManager manager = new ScriptEngineManager(); ScriptEngine engine = manager.getEngineByName( "JavaScript" ); System.out.println( engine.getClass().getName() ); System.out.println( "output:" + engine.eval( "function show() { return 10; }; show();" ) ); } } jdk.nashorn.api.scripting.NashornScriptEngine output:10
  • 20. 8. Parallel Array Sorting => As of now java 7 we already having Arrays.sort() method to sort objects now java 8 introduced parallel sorting which has more speed than arrays.sort() and follows Fork/Join framework introduced in Java 7 to assign the sorting tasks to multiple threads that are available in the thread pool. => Java 8 added parallel sorting functionalities to java.util. Arrays to take advantage of multithread machines
  • 21. 8. Parallel Array Sorting class ParallelArray{ public static void main(String[] args ){ int arr[]={1,4,2,8,5}; Arrays.parallelSort(arr); for(int i:arr){ System.out.println(i); } } }