HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode

Java 8 Tutorial

This Java 8 tutorial list down important Java 8 features with examples which were introduced in this release. All features have links to detailed tutorials such as lambda expressions, Java streams, functional interfaces and date time API changes.

Java SE 8 was released in early 2014. In java 8, most talked about feature was lambda expressions. It has many other important features as well such as default methods, stream API and new date/time API. Let’s learn about these new features in java 8 with examples.

Table of Contents

1. Lambda Expression
2. Functional Interface
3. Default Methods
4. Streams
5. Date/Time API Changes

1. Lambda Expression

Lambda expressions are not unknown to many of us who have worked on other popular programming languages like Scala. In Java programming language, a Lambda expression (or function) is just an anonymous function, i.e., a function with no name and without being bounded to an identifier. They are written exactly in the place where it’s needed, typically as a parameter to some other function.

The basic syntax of a lambda expression is:

either
(parameters) -> expression
or
(parameters) -> { statements; }
or 
() -> expression

A typical lambda expression example will be like this:

(x, y) -> x + y  //This function takes two parameters and return their sum.

Please note that based on type of x and y, method may be used in multiple places. Parameters can match to int, or Integer or simply String also. Based on context, it will either add two integers or concat two strings.

Rules for writing lambda expressions

  1. A lambda expression can have zero, one or more parameters.
  2. The type of the parameters can be explicitly declared or it can be inferred from the context.
  3. Multiple parameters are enclosed in mandatory parentheses and separated by commas. Empty parentheses are used to represent an empty set of parameters.
  4. When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses. e.g. a -> return a*a.
  5. The body of the lambda expressions can contain zero, one or more statements.
  6. If body of lambda expression has single statement curly brackets are not mandatory and the return type of the anonymous function is the same as that of the body expression. When there is more than one statement in body than these must be enclosed in curly brackets.

Read More: Java 8 Lambda Expressions Tutorial

2. Functional Interface

Functional interfaces are also called Single Abstract Method interfaces (SAM Interfaces). As name suggest, they permit exactly one abstract method inside them. Java 8 introduces an annotation i.e. @FunctionalInterface which can be used for compiler level errors when the interface you have annotated violates the contracts of Functional Interface.

A typical functional interface example:

@FunctionalInterface
public interface MyFirstFunctionalInterface {
	public void firstWork();
}

Please note that a functional interface is valid even if the @FunctionalInterface annotation would be omitted. It is only for informing the compiler to enforce single abstract method inside interface.

Also, since default methods are not abstract you’re free to add default methods to your functional interface as many as you like.

Another important point to remember is that if an interface declares an abstract method overriding one of the public methods of java.lang.Object, that also does not count toward the interface’s abstract method count since any implementation of the interface will have an implementation from java.lang.Object or elsewhere. for example, below is perfectly valid functional interface.

@FunctionalInterface
public interface MyFirstFunctionalInterface 
{
	public void firstWork();

	@Override
	public String toString();                //Overridden from Object class

	@Override
	public boolean equals(Object obj);        //Overridden from Object class
}

Read More: Java 8 Functional Interface Tutorial

3. Default Methods

Java 8 allows you to add non-abstract methods in interfaces. These methods must be declared default methods. Default methods were introduces in java 8 to enable the functionality of lambda expression.

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.

Let’s understand with an example:

public interface Moveable {
    default void move(){
        System.out.println("I am moving");
    }
}

Moveable interface defines a method move() and provided a default implementation as well. If any class implements this interface then it need not to implement it’s own version of move() method. It can directly call instance.move(). e.g.

public class Animal implements Moveable{
    public static void main(String[] args){
        Animal tiger = new Animal();
        tiger.move();
    }
}
 
Output: I am moving

If class willingly wants to customize the behavior of move() method then it can provide it’s own custom implementation and override the method.

Reda More: Java 8 Default Methods Tutorial

4. Java 8 Streams

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.

An example of stream API. In this example, items is collection of String values and you want to remove the entries that begin with some prefix text.

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

Here items.stream() indicates that we wish to have the data in the items collection processed using the Streams API.

Read More: Java 8 Internal vs. External Iteration

5. Java 8 Date/Time API Changes

The new Date and Time APIs/classes (JSR-310), also called as ThreeTen, which have simply change the way you have been handling dates in java applications.

Dates

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

  1. The LocalDate class represents a date. There is no representation of a time or time-zone.
  2. The LocalTime class represents a time. There is no representation of a date or time-zone.
  3. The LocalDateTime class represents a date-time. There is no representation of a time-zone.

If you want to use the date functionality with zone information, then Lambda provide you extra 3 classes similar to above one i.e. OffsetDate, OffsetTime and OffsetDateTime. Timezone offset can be represented in “+05:30” or “Europe/Paris” formats. This is done via using another class i.e. ZoneId.

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"));

Timestamp and Duration

For representing the specific timestamp ant any moment, the class needs to be used is Instant. The Instant class represents an instant in time to an accuracy of nanoseconds. Operations on an Instant include comparison to another Instant and adding or subtracting a duration.

Instant instant = Instant.now();
Instant instant1 = instant.plus(Duration.ofMillis(5000));
Instant instant2 = instant.minus(Duration.ofMillis(5000));
Instant instant3 = instant.minusSeconds(10);

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));

Read More: Java 8 Date and Time API Changes

Drop me your questions on this Java 8 tutorial in comments section.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.

Share this:

  • Twitter
  • Facebook
  • LinkedIn
  • Reddit

Feedback, Discussion and Comments

  1. Raghunathan

    May 8, 2020

    I installed Java 8 on my Windows 10 PC (JDK 14.0.1_64_bin.exe), and tried running a simple HelloWorld java program. It compiled but, in execution, is throwing a JNI error as below. Any tips?

    A JNI error has occurred, please check your installation and try again.
    Exception in thread “main” java.lang.UnsupportedClassVersionError: HelloWorld has been compiled by a more recent version of the JavaRuntime (class file version 50.0), this version of the JavaRuntime only recognizes class file version up to 52.0.

    I get the same error for any Java program.

    • Lokesh Gupta

      May 8, 2020

      Find all .class files (may be in “bin” or “target” folder) and delete them. Then recompile the java file and try again.

      • Raghu

        May 9, 2020

        I see no .class files in either folder.

        • Lokesh Gupta

          May 9, 2020

          File search within project folder or the path from where you are running the java command.

  2. sushil kumar verma

    March 10, 2020

    Hello buddy, I tried the example using java but when I try to instantiate then I get compile time error.
    Cannot instantiate the type Moveable

    • Penn.Zhang

      April 9, 2020

      Have you set the right java version , it would compile error if the java version lower than 8

  3. Yashpal Singh Savner

    January 4, 2020

    Thanks Dear! for this nice tutorial.

  4. Justin Raj S

    September 5, 2019

    Hi,
    Very much interested and useful posts. Special thanks to you. is there any video tutorial available on website? if yes, share those details please.

  5. Abhinav Srivastava

    June 15, 2019

    Mono<List> mp =	webClient.get().uri(accountMgmtURI)
    		.retrieve()
    		.bodyToMono(Map.class)
    		.flatMap(trans -> {
    			List content= (List) trans.get("content");
    			System.out.println("content :: "+content);
    			return Mono.just(content);
    		});
    
    		System.out.println("content :: "+mp.toString());
    );
    String	sites = mp.toString();
    
    • Abhinav Srivastava

      June 15, 2019

      can someone help me on this , new to spring reactive my api response is :

      {
      “version”: “1.0”,
      “content”: [
      “12345”,
      “67076”,
      “123462”,
      “604340”,
      “1331999”,
      “1332608”,
      “1785581”,
      ]
      }

      need the “content”(list of string) to “sites” (string pipe separated)

  6. sai vijay

    February 13, 2019

    Very nice tutorial. recommended to every one.thanks

  7. Sachin Sridhar

    January 6, 2019

    Very Nice. I will recommend it to everyone to read it.

  8. A User

    August 14, 2018

    Very nice tutorials for reading. Can you also provide a PDF of your whole java 8 tutorial, as it is much helpful to read in offline mode.

  9. Jack

    March 26, 2018

    Hi Lokesh,
    Thank you.

  10. Laxminarsaiah Ragi

    January 3, 2018

    Hi Lokesh need help, i have a method, that will return User Object.

    public User getUserById(Integer uid) {
    	return (User) userList.stream().filter(user -> user.getId().equals(uid));
    }

    But this code throwing java.util.stream.ReferencePipeline$2 cannot be cast to com.pi.user.User,

    how to return a object.

    • Lokesh Gupta

      January 3, 2018

      In this case, you can use short circuit operation. Try adding “.findFirst().get()”. e.g.

      public Employee getUserById(Integer uid) {
      	return (Employee) new ArrayList&amp;lt;Employee&amp;gt;().stream()
              .filter(user -&amp;gt; user.getId().equals(uid)).findFirst().get();
      }

      Read More: https://howtodoinjava.com/java8/java-streams-by-examples/#short_circuit_operations

  11. Place4Java

    December 8, 2017

    I’m beginner and your blogs are inspiration and I learnt a lot from it and I also just started a blog for java beginners at Place4Java.

Comments are closed on this article!

Search Tutorials

Java 8 Tutorial

  • Java 8 Features
  • Java 8 forEach
  • Java 8 Stream
  • Java 8 Boxed Stream
  • Java 8 Lambda Expression
  • Java 8 Functional Interface
  • Java 8 Method Reference
  • Java 8 Default Method
  • Java 8 Optional
  • Java 8 Predicate
  • Java 8 Regex as Predicate
  • Java 8 Date Time
  • Java 8 Iterate Directory
  • Java 8 Read File
  • Java 8 Write to File
  • Java 8 WatchService
  • Java 8 String to Date
  • Java 8 Difference Between Dates
  • Java 8 Join Array
  • Java 8 Join String
  • Java 8 Exact Arithmetic
  • Java 8 Comparator
  • Java 8 Base64
  • Java 8 SecureRandom
  • Internal vs External Iteration

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Java 15 New Features
  • Sealed Classes and Interfaces
  • EdDSA (Ed25519 / Ed448)