Java 8 Features Fundamentals

Posted By : Karan Singh Kanwal | 31-Jan-2020

Java

Loading...

java 8 fundamental features

Introduction

Java SE 8 was released in early 2014. In java 8, lambda expressions have gathered the maximum attention.

It consists of numerous other important features as well, such as default methods, stream API, and new date/time API.

1. Functional Interface

An interface that has only a single abstract method is called a functional interface.

It can have multiple default or static methods.

E.g. :

package com.test.Functional;

public interface IConnection {

public void connect();

default void print() {

System.out.println("in default method");

}

static void description() {

System.out.println("in static method");

}

}

A functional interface can have only one abstract method (method without body) and multiple default and static methods.

We can use the @FunctionalInterface annotation to prevent us from creating any other abstract method in our interface.

2. Lambda Expression

Lambda expression helps in functional programming and simplifying the development.

It is a shorter way of writing an implementation of a single abstract method interface. It is used to create anonymous functions.

E.g. :

private static void testRunnable() {

Runnable runnable = () -> { // Lambda expression

System.out.println("Inside lambda expression");

};

Thread thread = new Thread(runnable);

thread.start();

}

3. Optional

Optional is a simple container. It is a value that may be null or non-null.

Think of it like a method that may return a non-null result but sometimes returns nothing.

Instead of returning null, you return an Optional in Java 8 so to prevent NullPointer Exception. E.g.:

Optional<String> optional = Optional.of("bam");

optional.isPresent(); // true

optional.get(); // "bam"

optional.orElse("fallback"); // "bam"

optional.ifPresent((s) -> System.out.println(s.charAt(0))); // "b"

4. Default Methods

Java 8 allows you to add any non-abstract methods in interfaces. These methods must be declared default methods.

They were introduced in java 8 to enable the functionality of lambda expression.

They enable adding new functionality to the interfaces of your libraries and also ensure binary compatibility with code written for older versions of those interfaces.

E.g. :

public interface Moveable {

default void move(){

System.out.println("I am moving");

}

}

If any class is going to implement this interface then it need not implement the interfaces.

If a class has implemented this interface then it need not implement its own version of move() method.

It can directly call instance.move().

5. Java 8 Streams

Another major change introduced is Java 8 Streams API, which provides a mechanism for processing a set of data in various ways including filtering, transformation, or any other way that may be useful to an application.

Streams API in Java 8 support multiple types of iteration where you define the set of items to be processed, the operation(s) to be performed on each item in streams, and where the output of those operations is to be stored or shown.

E.g.:

List<String> items;

String prefix;

List<String> filteredList = items.stream().filter(e -> (!e.startsWith(prefix))).collect(Collectors.toList());

6. Java 8 Date/Time API Changes

The new Date and Time APIs/classes have been introduced and still haven't been used properly. Also called ThreeTen, they simply change the way you have been handling dates in java applications.

Dates :

Date class has become obsolete. The new classes replacing Date class are LocalDate, LocalTime, and LocalDateTime.

The LocalDate class represents a date. There isn't any representation of a time or time-zone.

The LocalTime class represents a time. There isn't any representation of a date or time-zone.

The LocalDateTime class represents a date-time. There is no representation of a time-zone.


The latest version of Java, Java 8 comprises new features, improvements, and fixed bugs for smooth development experience. As it supports JavaScript engine, functional programming, new APIs for streaming and date/time, and lambda expressions, it is the ideal choice for our developers at Oodles. We are an ERP development company that enables enterprises to streamline operations and drive returns, using next-gen technologies, with our development services. Our services include developing applications for all your ERP needs from CRM, WFM, and HRM to eCommerce, accounting, and wealth management software. We use an extensive tech stack including Java 8, Angular, and Spring to develop end to end customized software for your enterprise. Get in touch with our experts to know more about how you can implement these technologies into your business.