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.


import_contacts

You may also like:

Related Posts

  • Format Instant to String in java
    02 May

    How to format Instant to String in java

    Learn about how to format Instant to String in java.

  • Convert Date to LocalDate in java
    12 January

    Java Date to LocalDate

    In this post, we will see how to convert Date to LocalDate in java. Sometimes, we may need to convert Date to new Java 8 APIs and vice versa. There are multiple ways to convert Date to LocalDate in java. Read also: Convert LocalDate to Date in java Using toInstant() method of Date class You […]

  • Convert LocalDate to Date in java
    11 January

    Java LocalDate to Date

    In this post, we will see how to convert LocalDate to Date. Java 8 has introduced a lot of new APIs for Date and time. There can be many ways to convert Java LocalDateTime to date. Using Instant object You can convert LocalDate to Date using Instant object which we can from Zone. Here is […]

  • Convert Stream to List in java
    31 December

    Java Stream to List

    In this post, we will see how to convert Stream to List in java. There are multiple ways to convert Stream to List in java. Using Collectors.toList() You can pass Collectors.toList() to Stream.collect() method to convert Stream to List in java. Stream’s collect method performs mutable reduction operation on elements of Stream and Collectors.toList() provides […]

  • Convert LocalDateTime to Timestamp in java
    18 November

    Convert LocalDateTime to Timestamp in Java

    In this post, we will how to convert LocalDateTime to Timestamp . Before learning how to convert localdatetime to timestamp, let us learn about LocalDateTime and Timestamp, and understand the importance of this conversion. LocalDateTime LocalDateTime was introcuded in Java 8. LocalDateTime can be imported time package: import java.time.LocalDateTime; LocalDateTime is an immutable object used […]

  • 28 August

    A In-Depth guide to Java 8 Stream API

    In this post, we will see an in-depth overview of Java 8 streams with a lot of examples and exercises. Introduction You may think that Stream must be similar to InputStream or OutputStream, but that’s not the case. A Stream represents a sequence of elements supporting sequential and parallel aggregate operations. Stream does not store […]

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 *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.