2. Contents
• Brief introduction to Java
• Why Java 8
• Lambda expressions
• Streams
• Date and Time APIs
• Nashorn
• New APIs to join Strings
3. Brief introduction to Java
• Java is a high-level programming language
– A high-level language cannot run directly on a computer, it
needs to be translated into a machine language by a
program called Compiler
• Java source code is compiled to intermediate byte
code, which is interpreted and executed by a JVM
– A virtual machine is a software application that simulates a
computer but hides the underlying OS and hardware from
the programs that interact with it
• Java’s byte-codes are portable between platforms,
i.e., “Write Once, Run Anywhere”
• Java is a computing platform
– It consists of two essential software – JRE and JDK
4. Why Java 8 ?
• Java 8 is the latest version of Java, which contains
important enhancements to improve performance,
stability and security of Java applications
• The addition of Lambda expressions to the language
brings Java to the forefront of functional
programming
• New parallel processing capabilities are added to the
framework for efficiently carrying out heavy
operations such as sorting, filtering and mapping
• A new lightweight, high performance implementation
of JavaScript engine is integrated to JDK and is
available to Java applications via existing APIs
5. Lambda expressions
• Lambda expressions are anonymous functions
which are used to instantiate interfaces with single
abstract method
• Replaces more verbose class declarations and
eliminates unnecessary syntaxes
• Significantly reduces the amount of code and
makes it lot more readable
• Doesn't provide any performance benefit, as the
underlying functionality remains exactly the same
• Facilitates functional programming, and simplifies
the development a lot
6. Lambda expressions Syntax
• Syntax : ( parameters… ) -> { method body };
• Important characteristics of lambda expressions
– Optional type declaration − No need to declare the type of a
parameter, the compiler can inference the same from the
single abstract method in interface
– Optional parenthesis around parameter − No need to
declare a single parameter in parenthesis, for multiple
parameters, parentheses are required
– Optional curly braces − No need to use curly braces in
method body, if the body contains a single statement
– Optional return keyword − The compiler automatically
returns the value, if the expression has a single value to
return, curly braces are required in-case method body
explicitly returns a value
7. Functional Interfaces
• Where to use Lambda expression
– Lambda expressions can only appear in places where they
will be assigned to a variable whose type is a functional
interface
• Functional Interface
– A Functional Interface has a single abstract method (not
methods inherited from Object and declared as abstract)
– Instances of functional interfaces can be created with
lambda expressions, method references, or constructor
references
– Functional Interfaces included with Java runtime are –
Runnable, Callable, Comparator, Comparable, Iterable
– Java 8 added a number of new functional interfaces in
package java.util.function
8. Important FIs and Method
References
• forEach method
– In Java 8, we can use lambda expression to traverse
collection of items, mainly lists and sets
– New default implementation of forEach in Iterable interface
is - default void forEach(Consumer<? super T> action),
where Consumer is a FI with abstract method - accept(T t)
• Predicate FI
– Predicate FI has a single abstract method – test(T t), which
can be used to wrap up conditional processing and make
conditional code much cleaner
• Method References
– Method reference give us a way of naming a method that
we want to call instead of calling it directly, goal is to make
the code more concise and readable
9. Default and Static Methods in
Interface
• Prior to Java 8, interfaces can only contain abstract
methods and constant declarations, but Java 8 has
added support for default and static methods too
• Default Methods
– A default method is an instance method defined in an
interface whose method begins with the ‘default’ keyword
– Every class that implements the interface inherits the
interface's default methods and can override them
• Static Methods
– A static method defined inside an interface begins with the
‘static’ keyword
– Static method always remains as a part of the interface and
not part of the implementing classes
10. Java 8 Stream
• In Java 8, we have a new way of managing,
traversing, filtering and aggregating collections with
the Stream APIs
• A stream represents a sequence of elements and
supports different kind of operations to perform
computations upon those elements
• Java 8 supports two kinds of collection streams
known as Sequential stream and Parallel stream
• To avoid divide by zero problem in case of
averaging, we can use a type of Optional variable
• mapTo* - takes a complex object and extracts a
simple primitive value from it
11. Date and Time APIs
• Java 8 includes a complete set of new APIs for
managing date and time values
• The classes that actually hold the data in new Date
and Time APIs are all immutable and thread-safe, so
we don’t have to worry about passing the object
around multi-threading environment and while using
them with parallel stream
• LocalDate | LocalTime | LocalDateTime classes
simplify the development where timezones are not
required and ZonedDateTime class is used where
timezone needs to be considered
• All classes of these new APIs are members of the
java.time package
12. Nashorn
• Nashorn is a JavaScript engine developed in Java
programming language
• It allows to code in JavaScript instead of Java using
the dynamic coding capabilities that language
supports and we can write complete code blocks
using JavaScript and then execute them in the
context of Java class
• Its always a better practise to put all the JavaScript
code into a separate file
• The Nashorn JavaScript engine can either be used
programmatically from Java programs or by utilizing
the command line tool jjs, which is located in
$JAVA_HOME/bin
13. StringJoiner Class and
String.join Method
• StringJoiner class is used to construct a sequence
of characters separated by a delimiter and
optionally starting with a supplied prefix and ending
with a supplied suffix
• String.join method returns a String composed of
copies of the CharSequence elements joined
together with a copy of the specified delimiter
• String.join method is an abbreviated call in a sense
but under the hood it delegates to StringJoiner
class