Java 8 Flashcards
What are the new features
released in Java 8?
The new features released in Java 8 are:
- Lambda Expression
- Stream API
- Date and Time API
- Functional Interface
- Interface Default and Static Methods
- Optional
- Base64 Encoding and Decoding
- Nashorn JavaScript Engine
- Collections API Enhancements
- Concurrency Enhancements
- Fork/Join Framework Enhancements
- Spliterator
- Internal Iteration
- Type Annotations and Repeatable Annotations
- Method Parameter Reflection
- JVM Parameter Changes
What are the main benefits of new features introduced in Java 8?
The main benefits of Java 8 features are:
1. Support for functional programming by Lambda and
Streams
2. Ease of high volume data processing by Streams
3. Ease of use by getting Parameter names through Reflection
4. Reusable code with enhanced Collection APIs
5. Smart exception handling with Optional
6. Control on JVM with new Parameters
7. Enhanced encryption support with Base 64
8. Faster execution with Nashorn JavaScript engine support
What is a Lambda expression
in Java 8?
Lambda expression is an anonymous function. It is like a method
that does not need any access modifiers, name or return value
declaration. It accepts a set of input parameters and returns result.
Lambda expression can be passed as a parameter in a method. So
we can treat code in Lambda expression as data. This piece of code
can be passed to other objects and methods
What are the three main parts
of a Lambda expression in Java?
Three main parts of a Lambda expression are:
1. Parameter list: A Lambda expression can have zero or
more parameters. Parameter list is optional to Lambda.
2. Lambda arrow operator: “->” is known as Lambda arrow
operator. It separates the list of parameters and the body of
Lambda.
3. Lambda expression body: The piece of code that we want
to execute is written in Lambda expression body.
E.g. In following example:
Arrays.asList( “a”, “b”, “d” ).forEach( e -> System.out.println( e ) );
Parameter list = e
Arrow = ->
Body = System.out.println( e )
What is the data type of a
Lambda expression?
A Lambda expression fulfills the purpose of passing code as data.
The data type of a Lambda expression is a Functional interface.
In most of the cases this is java.lang.Runnable interface.
What are the advantages of a
lambda expression?
We can pass a lambda expression as an object to a method. This
reduces the overhead involved in passing an anonymous class.
We can also pass a method as a parameter to another method using
lambda expressions.
What is a Functional interface
in Java 8?
A Functional interface in Java is an interface that has exactly one
abstract method.
It can have default methods with implementation. A default method
is not abstract.
In Java 8, java.lang.Runnable and java.util.concurrent.Callable are
two very popular Functional interfaces
How can we define a Functional interface in Java 8?
To define a Functional interface in Java 8, we can create an
Interface with exactly one abstract method.
Another way is to mark an Interface with annotation
@FunctionalInterface. Even with the annotation we have to follow
the rule of exactly one abstract method.
The only exception to this rule is that if we override
java.lang.Object class’s method as an abstract method, then it does
not count as an abstract method.
Why do we need Functional
interface in Java?
Functional Interfaces are mainly used in Lambda expressions,
Method reference and constructor references.
In functional programming, code can be treated as data. For this
purpose Lambda expressions are introduced. They can be used to
pass a block of code to another method or object.
Functional Interface serves as a data type for Lambda expressions.
Since a Functional interface contains only one abstract method, the
implementation of that method becomes the code that gets passed as
an argument to another method
What are the main uses of Stream API in Java 8?
Main uses of Stream API in Java 8 are:
1. It helps in using data in a declarative way. We can make
use of Database functions like Max, Min etc., without
running a full iteration.
2. It makes good use of multi-core architectures without
worrying about multi-threading code.
3. We can create a pipeline of data operations with Java
Stream that can run in a sequence or in parallel.
4. It provides support for group by, order by etc. operations.
5. It supports writing for code in Functional programming
style.
6. It provides parallel processing of data.
What are the differences
between Intermediate and Terminal
Operations in Java 8 Streams?
Main differences between Intermediate and Terminal Stream
operations are as follows:
1. Evaluation: Intermediate operations are not evaluated until
we chain it with a Terminal Operation of Stream. Terminal
Operations can be independently evaluated.
2. Output: The output of Intermediate Operations is another
Stream. The output of Terminal Operations is not a Stream.
3. Lazy: Intermediate Operations are evaluated in lazy
manner. Terminal Operations are evaluated in eager
manner.
4. Chaining: We can chain multiple Intermediate Operations
in a Stream. Terminal Operations cannot be chained
multiple times.
5. Multiple: There can be multiple Intermediate operations in
a Stream operation. There can be only one Terminal
operation in Stream processing statement.
What is a Spliterator in Java 8?
A Spliterator is a special type of Iterator to traverse and partition
the elements of a source in Java. A source can be a collection, an IO
channel or a generator function.
A Spliterator may traverse elements individually or sequentially in
bulk.
What are the differences between Iterator and Spliterator in Java 8?
Main differences between Iterator and Spliterator are as follows:
1. Spliterator can be used with Streams in Java 8. Where as,
Iterator is just used with Collection.
2. Spliterator uses Internal Iteration to iterate Streams.
Iterator uses External Iteration to iterate Collections.
3. Spliterator can iterate Streams in Parallel as well as
Sequential manner. Iterator only iterates in Sequential
manner.
4. Spliterator can traverse elements individually as well as in
bulk. Iterator only iterates elements individually.
What is Type Inference in Java 8?
A Java compiler can see each method’s invocation and it
declaration to determine what are type arguments required for
invocation.
By Type Inference, Java can determine the types of the arguments as
well as the type of the result being returned.
Type inference algorithm also tries to find the most specific type
that can work with all types of arguments.
How does Internal Iteration work in Java 8?
In an Iterator, the fundamental question is that which party controls
the iteration. Is it Iterator or the Collection on which iterator runs.
When a Collection controls the iterator, then it is called External
Iteration. When the Iterator controls the iteration then it is called
Internal Iteration.
In case of Internal Iteration, the client hands over an operation to
Iterator and the Iterator applies the operation to all the elements in
aggregate.
Internal Iteration is easier to implement, since the Iterator does not
have to store the state of the collection.