Java Features by Version- Java 8

Java8 Features(2014)

Functional Interfaces (Java 8)

An Interface that contains exactly one abstract method is known as functional interface. It can have any number of default, static methods but can contain only one abstract method. It can also declare methods of object class

@FunctionalInterface  
interface sayable{  
    void say(String msg);  
}

Lambda Expressions (Java 8)

The Lambda expression is used to provide the implementation of an interface which has functional interface

Stream (Java 8)

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

Streams API in Java 8 supports a different type of iteration where you simply define the set of items to be processed, the operation(s) to be performed on each item, and where the output of those operations is to be stored.

Default Methods & Static methods Interface (Java 8)

Java 8 allows you to add non-abstract methods in interfaces. These methods must be declared default methods.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.

public interface Moveable {
    default void move(){
        System.out.println("I am moving");
    }
}
static void isNull(String str) {
 System.out.println("Interface Null Check"); 
}

If class willingly wants to customize the behavior of move() method then it can provide it’s own custom implementation and override the method.in this case we must remove default keyword.

Static methods Java interface static method is similar to default method except that we can’t override them in the implementation classes.

Java 8 Date/Time API (Java 8)

Date class has even become obsolete. The new classes intended to replace Date class are LocalDate, LocalTime and LocalDateTime.

  • The LocalDate class represents a date. There is no representation of a time or time-zone.

  • The LocalTime class represents a time. There is no representation of a date or time-zone.

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

    LocalDate localDate = LocalDate.now();
    LocalTime localTime = LocalTime.of(12, 20);
    LocalDateTime localDateTime = LocalDateTime.now();
    OffsetDateTime offsetDateTime = OffsetDateTime.now();
    ZonedDateTime zonedDateTime = ZonedDateTime.now(ZoneId.of("Europe/Paris"));
    

Duration class is a whole new concept brought first time in java language. It represents the time difference between two time stamps.

Duration duration = Duration.ofMillis(5000);
duration = Duration.ofSeconds(60);
duration = Duration.ofMinutes(10);

Duration deals with small unit of time such as milliseconds, seconds, minutes and hour. They are more suitable for interacting with application code. To interact with human, you need to get bigger durations which are presented with Period class.

Period period = Period.ofDays(6);
period = Period.ofMonths(6);
period = Period.between(LocalDate.now(), LocalDate.now().plusDays(60));