Java 8 includes several new features such as lambda expressions, default methods in interfaces, stream API for bulk data operations, date/time API improvements, and concurrency API enhancements. Some key additions are lambda expressions for functional-style programming, default/static methods allowing implementation code in interfaces, and the stream API providing a powerful way to process and filter collections of data.
2. Introduction of Java:
Java is a programming language created by James
Gosling from Sun Microsystems (Sun) in 1991. The
target of Java is to write a program once and then run
this pro
The current version of Java is Java 1.8 which is also
known as Java 8.
The Java language was designed with the following
properties:
1. Platform independent
2. Object-orientated programming language
3. Interpreted and compiled language
4. Automatic memory management
4. New Features in Java8:
forEach() method in Iterable interface.
default and static methods in interface.
Functional interfaces and lambda expression.
Java Stream API for Bulk Data Operation on
Collection.
Java Time API.
Collection API improvements.
Concurrency API improvements.
Java IO improvements.
5. forEach() method in Iterable interface:
Whenever we need to traverse through a Collection,
we need to create an Iterator whose whole purpose is
to iterate over
Java 8 has introduced forEach method
in java.lang.Iterable interface so that while writing
code we focus on logic only.
6. default and static methods in interface:
If you read forEach method details carefully, you will
notice that its defined in Iterable interface but we
know that interfaces cant have method body.
From Java 8, interfaces are enhanced to have method
with implementation. We can
use default and static keyword to create interfaces
with method implementation.
Static methods, by definition, are not abstract .
default void forEach(ClassName<? super T> action) {
}
7. Lambda Expression:
Implement Functional Programming
Lambda expressions provide anonymous function types to
Java.
Replace use of anonymous inner classes.
Provide more functional style of programming in
Java.
doSomething(new DoStuff() {
public boolean isGood(int value) {
return value == 42;
}
});
doSomething(answer -> answer == 42);
8. Lambda Expressions in GUI
Applications:
To process events in a graphical user interface (GUI) application, such as
keyboard actions, mouse actions, and scroll actions, you typically create
event handlers, which usually involves implementing a particular interface.
Often, event handler interfaces are functional interfaces; they tend to have
only one method.
btn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
System.out.println("Hello World!");
}
});
btn.setOnAction(
event -> System.out.println("Hello World!")
);
9. Functional Interface:
Single Abstract Method (SAM) type
A functional interface is an interface that has one
abstract method.
Represents a single function contract.
Doesnt mean it only has one method .
@FunctionalInterface annotation
Helps ensure the functional interface
contract is honoured
Compiler error if not a SAM
10. Extension Methods:
Bringing Multiple Inheritance (of Functionality) to Java
Provide a mechanism to add new methods to existing
interfaces
Without breaking backwards compatibility
Gives Java multiple inheritance of behavior, as well as
types (but not state!)
public interface Set extends Collection {
public int size();
... // The rest of the existing Set methods
public T reduce(Reducer r) default Collections.setReducer;
}
11. Java Stream API:
A new java.util.stream has been added in Java 8 to
perform filter/map/reduce like operations with the
collection.
This is one of the best feature because we work a lot with
Collections and usually with Big Data, we need to filter out
them based on some conditions.
Collection interface has been extended
with stream() and parallelStream() default methods to get
the Stream for sequential and parallel execution.
parallel processing will be very helpful while working with
huge collections.
12. Java Time API:
It has always been hard to work with Date, Time and
Time Zones in java. There was no standard approach
or API in java for date and time in Java.
One of the nice addition in Java 8 is the java.time
package that will streamline the process of working
with time in java.
One of the useful class is DateTimeFormatter for
converting date time objects to strings.
13. Collection API improvements:
Iterator default method
forEachRemaining(ClassName object) to perform the
given action for each remaining element until all elements
have been processed or the action throws an exception.
Collection default method removeIf(Predicate filter) to
remove all of the elements of this collection that satisfy the
given predicate.
Collection spliterator() method returning Spliterator
instance that can be used to traverse elements sequentially
or parallel.
Map replaceAll(), compute(), merge() methods.
14. Performance Improvement for HashMap
class with Key Collisions, how?
in case of collision till Java 7 it used to store values in
linked list and the search order for link list is O(n), but
in java 8 it forms binary tree (O(log(n))) instead of
linked list. This makes search faster, this would be
useful in case of billions of records getting collide for
same hash key.
15. Concurrency API improvements:
Files.list(Path dir) that returns a lazily populated
Stream, the elements of which are the entries in the
directory.
Files.lines(Path path) that reads all lines from a file as
a Stream.
Files.find() that returns a Stream that is lazily
populated with Path by searching for files in a file tree
rooted at a given starting file.
BufferedReader.lines() that return a Stream, the
elements of which are lines read from this
BufferedReader.
16. Nashorn JavaScript Engine:
Lightweight, high-performance JavaScript engine.
Integrated into JRE .
Use existing javax.script API.
New command-line tool, jjs, to run JavaScript.
It can run scripts as JavaFX applications
Native javascript arrays are untyped. Nashorn enables
you to use typed java arrays in javascript.
17. Some Other Features:
Comparator interface has been extended with a lot of
default and static methods for natural ordering,
reverse order etc.
min(), max() and sum() methods in Integer, Long and
Double wrapper classes.
logicalAnd(), logicalOr() and logicalXor() methods in
Boolean class.
JDBC-ODBC Bridge has been removed.