Java 8 tutorial

Java 8 Tutorial

In this post, we will see about Java 8 tutorial.
I have written a lot of tutorials on Java 8. This is index post for all Java 8 posts.

Java 8 has lots of new features which will change the way you do programming. Here is a list of features you need to know to work with Java 8 programming.


Lambda expression

Lambda expression is a representation of anonymous function which can be passed around. Lambda expression does not have name but it has parameters, body and return type.
Basic syntax of lambda expression is like below:

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

As you can see if you have multiple statements then you need to put curly braces.

Functional interface

The functional interface is the interface which can have only one abstract method.
Functional interface and lambda expression go hand in hand. You can write lambda expression for function interface.
For example:
Runnable interface

Default methods in interface

Java 8 has introduced a lot of new methods in interfaces but it might break older version code. If any class does not implement interface’s method then it gives compilation error. To tackle this issue, Java 8 has the introduction of default methods. It might sound surprising but interface can have implementation code as well. You can declare default method in the interface and it won’t force client to implement default method.
For example:
reversed() is default method in Comparator interface.

Static methods in interface

Similar to default methods, you can have static method too in the interface.
For example:
naturalOrder() is static method in Comparator interface.

Optional

Java 8 Optional can be used to avoid NullPointerException.I have written detailed Java 8 optional tutorial.

Stream API

Stream is one of the biggest addition to Java 8. Stream API help you to process data in declarative manner similar to SQL queries.
For example:
Let’s say you want to filter employees of age greater than 30 and create a list of names.

Create a class "EmployeeMain" which will use both conventional and java 8’s stream to filer list of employees.

When you run above program, you will get below output

Filtered list of employees using conventional way:
John
Mary
Gary
================================
Filtered list of employees using Stream :
John
Mary
Gary

As you can see, Stream can help you write code in consite and clean way.


Java 8 Date/Time

Java 8 has introduced new Date and time APIs. It is buillt to cover all the flaws of Date and Calendar APIs which we have been using for so long.
Let’s see some of new API classes.

LocalDate

LocalDate class represents Date only. There is no time or timezone information.

Output:

Year: 2018
Month: MAY
Day:20

LocalTime

LocalDate class represents Time only. There is no Date or timezone information.

Output:

Hour: 0
Minute: 32
Second:54

LocalDateTime

LocalDateTime class represents date and time both. There is no timezone information.

Output:

Year: 2018
Month: MAY
Day:20
Hour: 0
Minute: 35
Second:40

Instant

Instant class is used to represent time stamp in java.You can perform plus, minus and various other operations on Instant.

Output:

Instant 1: 2018-05-19T19:10:23.540064Z
Instant 2: 2018-05-19T19:10:28.540064Z

Duration

Duration class represents time interval between two instants in second or milliseconds.

Output:

Duration in seconds: 60

That’s all about Java 8 tutorial.

Was this post helpful?

Comments

  1. Nice post, easy to read and understand.
    I have only one comment about fixing the link of Interface static methods in java 8.

    many thanks

Leave a Reply

Your email address will not be published. Required fields are marked *