Java8 Tutorial
Java8 Tutorial
Java8 Tutorial
Java 8 is the most awaited and is a major feature release of Java programming language.
This is an introductory tutorial that explains the basic-to-advanced features of Java 8 and
their usage in a simple and intuitive way.
Audience
This tutorial will be useful for most Java developers, starting from beginners to experts.
After completing this tutorial, you will find yourself at a moderate level of expertise in Java
8, from where you can take yourself to next levels.
Prerequisites
Knowledge of basic Java programming language is the only prerequisite for learning the
concepts explained in this tutorial.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent
of the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our
website or its contents including this tutorial. If you discover any errors on our website or
in this tutorial, please notify us at contact@tutorialspoint.com
i
Table of Contents
About the Tutorial .................................................................................................................................... i
Audience .................................................................................................................................................. i
Prerequisites ............................................................................................................................................ i
Syntax ..................................................................................................................................................... 5
Scope ...................................................................................................................................................... 7
ii
7. JAVA 8 ─ STREAMS............................................................................................................. 20
forEach .................................................................................................................................................. 21
map ....................................................................................................................................................... 21
filter ...................................................................................................................................................... 21
limit....................................................................................................................................................... 21
sorted.................................................................................................................................................... 22
Collectors .............................................................................................................................................. 22
Statistics ................................................................................................................................................ 22
jjs .......................................................................................................................................................... 33
iii
Temporal Adjusters ............................................................................................................................... 42
Methods................................................................................................................................................ 45
iv
1. JAVA 8 ─ OVERVIEW
JAVA 8 is a major feature release of JAVA programming language development. Its initial
version was released on 18 March 2014. With the Java 8 release, Java provided supports for
functional programming, new JavaScript engine, new APIs for date time manipulation, new
streaming API, etc.
New Features
Lambda expression - Adds functional processing capability to Java.
New tools - New compiler tools and utilities are added like ‘jdeps’ to figure out
dependencies.
Stream API - New stream API to facilitate pipeline processing.
import java.util.Collections;
import java.util.List;
import java.util.ArrayList;
import java.util.Comparator;
5
names1.add("Naresh ");
names1.add("Kalpesh ");
Here the sortUsingJava8() method uses sort function with a lambda expression as
parameter to get the sorting criteria.
7
2. JAVA 8 ─ ENVIRONMENT SETUP
For most of the examples given in this tutorial, you will find a Try it option in our website
code sections at the top right corner that will take you to the online compiler. So just make
use of it and enjoy your learning.
Follow the instructions to download Java, and run the .exe to install Java on your machine.
Once you have installed Java on your machine, you would need to set environment variables
to point to correct installation directories.
3. Now, alter the 'Path' variable so that it also contains the path to the Java executable.
For example, if the path is currently set to 'C:\WINDOWS\SYSTEM32', then change
your path to read 'C:\WINDOWS\SYSTEM32;c:\Program Files\java\jdk\bin'.
Edit the 'C:\autoexec.bat' file and add the following line at the end:
'SET PATH=%PATH%;C:\Program Files\java\jdk\bin'
For example, if you use bash as your shell, then you would add the following line at the end
of your '.bashrc: export PATH=/path/to/java:$PATH'
Notepad: On Windows machine, you can use any simple text editor like Notepad
(recommended for this tutorial) or TextPad.
Eclipse: It is also a Java IDE developed by the Eclipse open-source community and
can be downloaded from http://www.eclipse.org/.
9
3. JAVA 8 ─ LAMBDA EXPRESSIONS
Lambda expressions are introduced in Java 8 and are touted to be the biggest feature of Java
8. Lambda expression facilitates functional programming, and simplifies the development a
lot.
Syntax
A lambda expression is characterized by the following syntax.
Optional curly braces - No need to use curly braces in expression body if the body
contains a single statement.
Optional return keyword – The compiler automatically returns the value if the body
has a single expression to return the value. Curly braces are required to indicate that
expression returns a value.
Java8Tester.java
public class Java8Tester {
public static void main(String args[]){
Java8Tester tester = new Java8Tester();
//with parenthesis
GreetingService greetService1 = message ->
System.out.println("Hello " + message);
//without parenthesis
GreetingService greetService2 = (message) ->
System.out.println("Hello " + message);
greetService1.sayMessage("Mahesh");
greetService2.sayMessage("Suresh");
}
interface MathOperation {
int operation(int a, int b);
}
interface GreetingService {
void sayMessage(String message);
}
C:\JAVA>javac Java8Tester.java
C:\JAVA>java Java8Tester
10 + 5 = 15
10 - 5 = 5
10 x 5 = 50
10 / 5 = 2
Hello Mahesh
Hello Suresh
Lambda expression eliminates the need of anonymous class and gives a very simple
yet powerful functional programming capability to Java.
Scope
Using lambda expression, you can refer to any final variable or effectively final variable (which
is assigned only once). Lambda expression throws a compilation error, if a variable is assigned
a value the second time.
Scope Example
Create the following Java program using any editor of your choice in, say, C:\> JAVA.
Java8Tester.java
12
public class Java8Tester {
final static String salutation = "Hello! ";
public static void main(String args[]){
GreetingService greetService1 = message ->
System.out.println(salutation + message);
greetService1.sayMessage("Mahesh");
}
interface GreetingService {
void sayMessage(String message);
}
}
C:\JAVA>javac Java8Tester.java
C:\JAVA>java Java8Tester
Hello! Mahesh
13
4. JAVA 8 ─ METHOD REFERENCES
Method references help to point to methods by their names. A method reference is described
using "::" symbol. A method reference can be used to point the following types of methods:
Static methods
Instance methods
Java8Tester.java
import java.util.List;
import java.util.ArrayList;
public class Java8Tester {
names.forEach(System.out::println);
}
}
C:\JAVA>javac Java8Tester.java
C:\JAVA>java Java8Tester
Mahesh
Suresh
Ramesh
Naresh
Kalpesh
15
End of ebook preview
If you liked what you saw…
Buy it from our store @ https://store.tutorialspoint.com
16