What are the main features of Java 8 ?

From 2019, Oracle announces a new version of Java between every 6 months interval and the current version of Java is 14 and they would soon release Java 15. However, Java got its major changes or upgrades with Java 8 which was released back in March 2014.

Prior to Java 8, Java was an object-oriented programming language. Comparing to Java, languages like Python and Scala require less code as they support functional programming and on other hand, Java requires class and object to do the same task and requires more lines of code.

Due to this reason, people started shifting to these type of languages.

In order to compete with others, it was essential to have introduced support for functional programming

Looking at this, Java developers came to the decision of enabling functional programming in Java, focusing on concise code structure. As a result, Java 8 came up with major features to support functional programming.

These features are the main features introduced in Java 8 :

  • Lambda Expression and Functional Interface
  • Stream API
  • Introduction of Default Methods and Static Methods in an Interface
  • Nashorn Javascript Engine
  • Predefined Functional Interface

 Lambda Expressions and Functional Interface

One of the main features of Java 8 is Lambda expression which opens the gate for functional programming. It works in companion with a functional interface. A lambda expression can be defined as an anonymous function that represents an instance of a functional interface.

On the other hand, a functional interface is an interface that consists exactly one abstract method that later, referenced with lambda expression.

@FunctionalInterface annotation is used to tell the compiler that this interface is intended to be functional interface, if any criteria get violated the compiler would trigger compile-time error. It is not mandatory to put the annotation, indeed, it is a good practice though.

It concise and reduces the complexity of the code.

Stream API

java.util.stream package brought in Java 8 introduces the concept of stream. It accepts a collection of objects either sequentially or in a parallel manner and forms a stream where each element go through intermediate operations and the terminal operation.

 

Introduction of Default Methods and Static Methods in an Interface

After Java 8, the interface now supports non-abstract methods and static methods. Non-abstract method, we mean default methods.

A default method allows adding a new feature in form of a method without affecting the implementing classes.

It means that classes that implement that interface are not compulsorily required to override the default method but a specific class that required it can.

Another feature was the Static method, now one can also write a static method within an interface. But it doesn’t directly available to implementing classes, they are required to call by using interface name. for example interfaceName.method()

Nashorn JavaScript Engine

Now an improved JavaScript engine, named Nashorn is proposed in Java 8 that provides better performance than already existing Rhino. It is used to allow embedding JavaScript in Java-based applications. One can be used it either through command-line tool or programmatically

However, It would soon be deprecated and GraalVM would replace it

Predefined Functional Interface

Java provides a bunch of predefined functional interface that resides in java.util.function package. Each and every predefined functional interface has its own characteristics. Some of these are predicate, consumer, supplier and function.

  • Predicate : For conditional checks
  • Function : For input processing
  • Consumer : For consuming input
  • Supplier : For supplying output

 

Leave a Reply