EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Java 8 Tutorial What’s New in Java 8?
Secondary Sidebar
Java Technology Tutorial
  • Java 7, Java 8 and Java 9
    • Java 7 Features
    • What's New in Java 8
    • Java 8 Features
    • How to Install Java 8
    • Java 8 Collections
    • Java 8 Collectors
    • Optional Class in Java 8
    • Java 8 Stream
    • Java 8 Interview Questions
    • Java 9 Features
  • Java servlet
    • What is Servlet
    • Servlet API
    • JAX-RS Jersey
    • Svelte Components
    • Java collection cheat sheet
    • Java collection hierarchy
    • Java collection api
    • Servlet Life Cycle
    • ServletContext
    • Servlet Filter
    • Session in Servlet
    • Session Bean
    • Entity Beans
    • Entity Framework Insert
    • What is JDB
    • Java Servlet Interview Questions
    • Servlet Types
    • Servlet Listener
    • Servlet Interview Questions
  • JSP
    • JSP Page
    • JSP if else
    • JSP property
    • JSP request
    • JSP format
    • What is JSP
    • JSP in Java
    • JSP Life Cycle
    • JSP?Architecture?
    • JSP Objects
    • Custom Tags in JSP
    • JSP Tag Library
    • JSP Directives
    • JSP Scripting Elements
    • JSP foreach
    • JSP Checkbox
    • JSP Include
    • JSP Taglib
    • JSP Scriptlet
    • JSP Declaration
    • Servlet JSP
    • JSP Redirect
    • JSP Forward
    • JSP Filters
    • JSP Formatter
    • JSP Expression
    • JSP File
    • Cookies in JSP
    • JSP Login Page
    • JSP Error Page
    • JSP getParameter
    • JSP Session
    • JSP Alert
    • JSP Implicit Objects
    • JSP Interview Questions
  • EJB
    • What is EJB
    • EJB Architecture
    • EJB Interview Questions
  • Java awt
    • What is AWT in Java
    • Java ActionListener
    • Java AWT Controls
    • Java MouseListener
    • Cron Scheduler in Java
    • JavaMail Api
    • Robot Framework with Java
    • Java Mail Properties
    • Java collection set
    • Java WindowListener
    • Java KeyListener
    • Java ItemListener
    • ScrollBar in Java
  • JSF
    • What is JSF
    • JSF Life Cycle
    • JSF Components
    • JSF Validation
    • JSF Interview Questions
  • JPA
    • What is JPA?
    • Java Persistence API
    • JPA Annotations
    • JAX-RS
    • What is ORM?
    • JPQL
  • JSTL
    • What is JSTL
    • JSTL Tags
    • JSTL replace
  • J2EE
    • What is JEE?
    • J2EE Framework
    • J2EE Architecture
  • Struct
    • What is Struts?
    • Struts Framework
    • Struts Architecture
  • Ant
    • Apache Ant
    • RESTful Services
    • Nutch Apache
    • Applet Life Cycle
    • Apache POI Dependency
  • JDBC
    • JDBC Batch Update
    • JDBC Insert
    • JDBC PreparedStatement
    • JDBC MySQL Driver
    • JFreeChart
    • JDBC Update
    • JDBC template
    • JDBC transaction
    • JDBC Types
    • JDBC Batch Insert
    • JDBC getConnection
    • JDBC connector
    • JDBC datasource
    • JDBC fetch size
    • JDBC resultset
    • JDBC Statement

Related Courses

Java Servlet Training Course

JavaFX Training Course

Jmeter Testing Training Course

What’s New in Java 8?

By Princy VictorPrincy Victor

What's New in Java 8?

Introduction to Java 8

As we all know, Java is one of the most powerful programming languages. A new version of Java, Java SE 8 has been released by Oracle on March 18, 2014. This innovative release of Java is mainly used for software development platforms. There are several features in Java 8 which are the updates to the existing version of programming libraries, JVM and Tools. The most important feature is said to be the introduction of lambda expressions which we will see along with other features.

New Java 8

For Java programming, Java SE 8 offers a plethora of features.

  • Lambda expressions
  • Stream API
  • ForEach() Method
  • Default Method
  • Date Time API
  • Nashorn JavaScript Engine
  • Method References
  • StringJoiner

1. Lambda Expression

Lambda expression is an anonymous function(a function without a name) that helps in writing code in a certain functional style. Single Abstract Method (SAM) can be implemented in a clear and concise way by using an expression. Since data can be iterated, filtered and extracted, it is very helpful, especially in the collection library. Lambda expression also helps in reducing the complexity of code. Below is the syntax of a lambda expression:

Syntax:

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

(Parameters) -> Expression

Code:

( a, b ) -> a + b // Expression takes 2 parameters and return the sum.

2. Stream API

Stream API offers a technique for data processing in different ways such as filtering, extracting, transformation, etc. with the help of package java.util.stream. There is no need to get confuse java.util.streams with Java InputStreams or OutputStreams as there is no relation between them. One of the main advantages of Stream API is that it does not alter its source. i.e If a set of data is filtered, a new set of data is created with filtered data rather than modifying the original source. Stream API evaluates the code only when it is needed and it does not iterate the code more than once. If reiteration has to be done, a new stream has to be generated. Several pre-defined methods are present to support this. To iterate the stream, the following code can be used.

Code:

Stream.iterate(1, elem->elem+1)

3. ForEach() Method

In order to iterate over the Collection Framework, Java SE8 offers a new method called forEach which is defined in the Iterable interface. ForEach() loop can be used in a collection class that extends the interface Iterable. Since this method inputs a single parameter, a lambda expression can also be passed as a parameter.

Code:

age.forEach( age -> {  System.out.println(age); }); //each age is taken and printed

4. Default Method

Normally, Non-abstract methods cannot be added to interfaces. But, in the case of Java 8, it permits to add those methods in interfaces. These methods are written with a keyword default and known as default methods. Since they are non-abstract methods, the method body can also be included. Interestingly, this feature also ensures the binary compatibility with older versions of code.

Code:

public interface Moveable {
default void Sound(){
System.out.println("Hi Anna, How is my sound?");
}}

5. Date Time API

In Java 8, a new Time API and Date API has been introduced where handling dates are in a different method compared to other versions of Java. These classes are also called as JSR-310, ThreeTen.

The following are the Time and Date classes that are available in Java. time package:

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)
  • Jtime.LocalDate class
  • LocalTime class
  • LocalDateTime class
  • MonthDay class
  • OffsetTime class
  • OffsetDateTime class
  • Clock class
  • ZonedDateTime class
  • ZoneId class
  • ZoneOffset class
  • Year class
  • YearMonth class
  • Period class
  • Duration class
  • Instant class
  • DayOfWeek enum
  • Month enum

Code:

Clock cl = Clock.systemDefaultZone();
System.out.println(cl.getZone());

6. Nashorn Javascript Engine

Nashorn is a JavaScript engine that helps in executing JavaScript code in Java Virtual Machine (JVM) dynamically. It can be done using the two methods mentioned below.

  • With the help of the command-line tool jjs.
  • By setting in it into Java source code.

In order to execute using the jjs command-line tool, the following steps can be performed.

  • Create a .js file js.
  • Write and save the following code into the file.

Code:

var welcome = function(){
print("welcome to Nashorn Javascript Engine");
};
welcome ();
  • The open command line terminal.
  • Write command jjs welcome.js and click enter.
  • Once the command is executed, the below output will be displayed.

Output:

What's New in Java 8?

7. Method References

Method References is another feature that is introduced in Java 8 that can be used in functional interface methods. In another way, it can be said that they are a subset of another java feature, lambda expressions. It is because a method reference can also be used if a lambda expression can be used.

Methods can be:

  • Reference to Constructor
  • Reference to Static method
  • Reference to an instance method

The reference to the Static method can be as shown below

Code: 

Thread t=new Thread(ExampleClass::ThreadStatusMethod);
t.start();

8. StringJoiner

A new final class StringJoiner has been added to Java 8 in the java. util package. A sequence of characters can be constructed separated by delimiters like comma (,), hyphen (-), etc.

Code:

StringJoiner Names = new StringJoiner("-"); // Here, delimiter is -
// Adding names  to StringJoiner
joinNames.add("Anna");
joinNames.add("Adam");

Output:

What's New in Java 8? 2

Security Enhancements

In addition to these features, several other security enhancements are also done to Java SE8.

  • The Public Key Cryptography Standards 11 (PKCS) has been extended to comprise 64-bit support for the Operating system, Windows.
  • For UNIX platforms, two new implementations have been presented. It offers blocking and non-blocking behavior.
  • In SunJCE provider, AES and PBE algorithms such as PBEWithSHA256AndAES_128 and PBEWithSHA512AndAES_256 are also added.
  • Java SE 8 supports the Server Name Indication (SNI) extension that extends the TLS/SSL protocols to connect during handshaking for supporting server applications.

Conclusion

Java SE8 is the new version of Java developed by Oracle which offers several features. Lambda expression is considered as the most significant feature among them. In this document, features of Java SE 8 and security enhancements are explained in detail.

Recommended Articles

This is a guide to What’s New in java 8? Here we discuss the different eight features of new java 8. You can also go through our other related articles to learn more-

  1. Arrays in Java Programming
  2. Radix Sort Java
  3. Optional Class in Java 8
  4. Daemon Thread in Java
Popular Course in this category
Java Training (41 Courses, 29 Projects, 4 Quizzes)
  41 Online Courses |  29 Hands-on Projects |  305+ Hours |  Verifiable Certificate of Completion
4.8
Price

View Course
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more