Java 8 – Features

October 29, 2018 Off By ravi24kant

This Java tutorial list down necessary Java 8 features with examples that were introduced during this release. All options have links to elaborate tutorials like lambda expressions, Java streams, functional interfaces and date time API changes.

Java SE 8 was released in early 2014. In java 8, most talked concerning feature was lambda expressions. it’s several alternative necessary features as well like default methods, stream API and new date/time API. Let’s find out about these new options in java 8 with examples.

Functional Interface

Functional interfaces are known as Single Abstract method interfaces (SAM Interfaces). As name recommend, they allow precisely one abstract methodology within them. Java 8 introduces an annotation i.e. @FunctionalInterface which may be used for compiler level errors once the interface you’ve got annotated violates the contracts of functional Interface.

A typical example of functional interface example:

public interface MyFunctionalInterface
Please note that a functional interface is valid even though the @FunctionalInterface annotation would be omitted. it’s just for informing the compiler to enforce single abstract methodology within interface.

Also, since default methods don’t seem to be abstract you’re liberal to add default methods to your functional interface as several as you prefer.

Another necessary purpose to recollect is that if an interface declares an abstract method overriding one among the general public methods of java.lang.Object, that also doesn’t count toward the interface’s abstract method count since any implementation of the interface can have an implementation from java.lang.Object or elsewhere. as an example, below is absolutely valid functional interface.

public interface MyFunctionalInterface
    public void Work();
    public String toString();                //Overridden from Object class
    public boolean equals(Object obj);        //Overridden from Object class

Lambda Expression

Lambda expressions aren’t unknown to several folks WHO have worked on alternative well-liked programming languages like Scala. In Java programing language, a Lambda or function) is simply an anonymous function, i.e., a function with no name associated while not being finite to an identifier. they’re written specifically within the place wherever it’s required, generally as a parameter to another function.

The basic syntax of a lambda expression is:

(parameters) -> expression
(parameters) ->
() -> expression

A typical lambda expression example are like this:

(a, b) -> a + b //This function takes 2 parameters and come back their total.

Please note that supported style of “a” and “b”, methodology is also used in multiple places. Parameters will match to int, or integer or just String additionally. based on context, it’ll either add 2 integers or concat 2 strings.

Lambda expressions rules

  • A lambda expression will have zero, one or a lot of parameters.
  • The type of the parameters is explicitly declared or it is inferred from the context.
  • Multiple parameters are enclosed in necessary parentheses and separated by commas. Empty parentheses are used to represent an empty set of parameters.
  • When there’s one parameter, if its kind is inferred, it’s not necessary to use parentheses. e.g. x -> return x*x.
  • The body of the lambda expressions will contain zero, one or a lot of statements.
  • If body of lambda expression has single statement curly brackets aren’t necessary and therefore the return type of the anonymous method is that the same as that of the body expression. once there’s over one statement in body than these should be boxed in curly brackets.

Default Methods

java 8 new feature “Default Methods” in interfaces. Before Java 8, the interface solely contains method signatures. With Java 8 new feature Default methods you’ll be able to include method body inside the interface.

Simple example:

package com.ravivermajavablog.defaultM;
public interface MyFirstInterface {
     default void dMethod(){
        System.out.println("from MyFirstInterface...");

OK, what’s default here? this is the new approach of declaring the method body in Java 8 for AN interface. As seen above, default methods comes together with implementation

Want to learn more Click here

Java 8 Stream

The Java 8 API adds a new abstraction called Stream that lets you process data in a declarative way.

Stream uses a straightforward way to query data from a database using SQL statements to provide a high-level abstraction of Java set operations and expressions.

The Stream API can greatly increase the productivity of Java programmers, allowing programmers to write efficient, clean, and concise code.

This style treats the set of elements to be treated as a stream, which is transmitted in the pipeline and can be processed on the nodes of the pipeline, such as filtering, sorting, and aggregation.

The element flow is processed in the pipeline through an intermediate operation, and finally the result of the previous processing is obtained from the terminal operation

stream of elements +-----> | filter +-> | sorted +-> | map +-> | collect |

Want to learn in detail Click here