Java 8 features with examples

This blog walks you through various Java 8 features with lucid examples which will be helpful for beginners as well as experienced developers. Every java developer must learn java 8 features in details. Because Java 8 has been released with many important features such as functional programming, default methods, stream API, lambdas, new date/time API and so on.

Furthermore it is recommended to have basic knowledge of Java programming. If you are new to Java please go through any of the Java tutorials available online.

Java 8 was released in 2014.Java 8 was a major release of Java programming language after 1.5. Functional programing feature of java 8 provides more flexibility and faster code development.

Java 8 feature

  • Functional Interface (@FunctionalInterface) :

    Also known as Single Abstract Method interfaces (SAM Interfaces). Interface having exact one abstract method can be marked as functional interface. @FunctionalInterface annotation ensure that we have exact one abstract method.
    First of all this is not new concept of Java 8 however, new annotation @FunctionalInterface is added as java 8 feature.
    Examples : Comparator, Runnable, etc

  • Lambda Expression

    : Functional interfaces can be instantiated using lambda. It is an anonymous function of functional interface implementation. Which can be used to implement functional programming.
    Lambda expression is the short way of method writing.
    One of the most famous and commonly used java 8 feature is Lambda expression.
    Example : a -> a*a; or (String s) -> Long.parseLong(s)

  • Method reference:

    Method reference in java 8 allows us to refer methods with class and method name directly. These reference variables can be used as parameters.
    There are manly 4 different ways of method references in java
    Example : System.out::println , Long::parseLong

  • Default Methods:

    Interface can have method implementation known as default method.
    It is not mandatory to override the default method in implementing class. Though the method is similar to normal method, it has some limitations.

  • Java Streams API

    :New stream APIs are used to facilitate pipeline processing.
    Java stream produces pipe-lined data to perform different operations on group of elements. Streams API is not data structure.
    Java stream involves creation of stream, performing intermediate operations followed by terminal operations on the stream.

  • Optional:

    New class added that lays emphasis on best practices to handle null values properly. It can have null or some value.
    Used to represent Optional object or empty value instead null reference.
    It works as container or wrapper for actual value.
    The option object is very helpful to avoid null pointer exception.
    A most noteworthy point about Optional class is that it has private constructor so we can not create object using new keyword.

  • JavaScript Engine:

    Java-based engine added to execute JavaScript code.

  • Date/Time:

    Date and time APIs are improved

Fast track reading :

  • Java 8 was a release with so many new features
  • @FunctionalInterface annotation was added in java 8 release
  • Lambda Expression support is added in java 8
  • Functions can be referred using reference variables
  • Interface can have method implementation known as default method
  • Stream APIs are added for process group of elements, like array, list etc
  • Optional class is introduced to wrap object to avoid null reference