Java 8 new features
Java Platform, Standard Edition 8 is a major feature release. This document summarizes features and enhancements in Java SE 8 and in JDK 8.
Lambda Expressions, a new language feature, has been introduced in this release. They enable you to treat functionality as a method
argument, or code as data. Lambda expressions let you express instances of single-method interfaces more compactly.
Click here for more details.
Java 8 introduced a new feature called "Method Reference". It is used to refer methods of functional interfaces.
They are effectively a subset of lambda expressions, because if a lambda expression can be used, then it might be possible to
use a method reference, but not always. Click here for more details.
Default methods enable you to add new functionality to the interfaces of your libraries and ensure binary compatibility
with code written for older versions of those interfaces. Click here for more details.
Java 8 provides a new method called forEach() to iterate over collection framework. It is defined in Iterable and Stream interface. It is a default method
defined in the Iterable interface. Collection classes which extends Iterable interface can use forEach loop to iterate elements. Here are the examples:
- Java 8 forEach example with Map
- Java 8 forEach example with List
First of all, please note that "Streams are not collections". java.util.stream is introduced to process
elements in sequence. Also don't get confused with InputStream, java.util.stream doesnot have any relationship with InputStream or
OuputStreams. java.util.stream are part of functional programming. Click here for more details.
Date and Time API
Prior to Java 8, Java Date and Time has below drawbacks:
- The existing classes java.util.Date and SimpleDateFormatter are not thread-safe.
- Poor API design. For example, years in java.util.Date start at 1900, months start at 1, and days start at 0—not very intuitive.
Java SE 8 will ship with a new date and time API in java.time that offers greatly improved safety and functionality
for developers. The new API models the domain well, with a good selection of classes for modeling a wide variety of developer
use cases. Click here for more details