Java8
Java8
1. Lambda Expression
2. Stream API
3. Date and Time API
4. Functional Interface
5. Interface Default and Static Methods
6. Optional
7. Base64 Encoding and Decoding
8. Nashorn JavaScript Engine
9. Collections API Enhancements
10. Concurrency Enhancements
11. Fork/Join Framework Enhancements
12. Spliterator
13. Internal Iteration
14. Type Annotations and Repeatable Annotations
15. Method Parameter Reflection
16. JVM Parameter Changes
351. What are the main benefits of new features introduced 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.
( e -> System.out.println( e ) );
This Lambda expression takes a parameter e and prints it via System.out.
356. Why did Oracle release a new version of Java like Java 8?
The main theme of Java 8 is support for functional programming. With increase in
Database size and growth of multi-code CPU servers, there is need for Java to
support such large-scale systems.
Lambda expressions are very useful for cloud computing where we can pass code
as data and run the same code on multiple servers.
Optional is a best practice that is borrowed from Google Guava library for handling
the exceptional cases. This has made programs more robust with support for edge
cases.
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 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.
363. What are the differences between Collection and Stream API
in Java 8?
1. Version: Collection API is in use since Java 1.2. Stream API is recent
addition to Java in version 8.
2. Usage: Collection API is used for storing data in different kinds of data
structures. Stream API is used for computation of data on a large set of
Objects.
3. Finite: With Collection API we can store a finite number of elements in
a data structure. With Stream API, we can handle streams of data that
can contain infinite number of elements.
4. Eager vs. Lazy: Collection API constructs objects in an eager manner.
Stream API creates objects in a lazy manner.
5. Multiple consumption: Most of the Collection APIs support iteration
and consumption of elements multiple times. With Stream API we can
consume or iterate elements only once.
364. What are the main uses of Stream API in Java 8?
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.
368. 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.
Yes, Java 7 supports Type Inference. In Java 8, Oracle has enhanced the Type
Inference concept. Now it can be used to define Lambda expressions, functions and
Method references.
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.
371. What are the main differences between Internal and External
Iterator?
Internal Iteration has many advantages over External Iteration. But it has one big
disadvantage. Since Java API is responsible for iterating in Internal iterator,
developer does not get any control over iteration.
375. Can we provide implementation of a method in a Java
Interface?
In this way, this implementation of the method becomes default behavior for any
class implementing the interface.
Let say there is an interface Car that is implemented by BMW, Chevrolet and
Toyota classes. Now a Car needs to add capability for flying. It will require change
in Car interface. Some of the car classes that do not have flying capability may fail.
Therefore a Default Implementation of flying methods is added in Car interface so
that cars with no flying capability can continue to implement the original Car
interface.
378. What is the purpose of a Static method in an Interface in Java
8?
1. Single Class: There is no need to create a separate Utils class for storing
utility or helper methods. We can keep these methods in same interface.
2. Encapsulation: With Static methods, complete behavior of a Class is
encapsulated in same class. There is no need to maintain multiple
classes.
3. Extension: It is easier to extend a Class/API. If we extend a collection
ArrayList, we get all the methods. We need not extend Collections class
also.
379. What are the core ideas behind the Date/Time API of Java 8?
There are three core ideas behind the Date/Time API of Java 8:
Some of the advantages of Java 8 Date Time API over existing Date API are:
Better Design: Date/Time classes prior to Java 8 have poor API design. For
example, years in java.util.Date start at 1900, months start at 1, and days start at 0.
It is not very intuitive. Java 8 Date Time API handles it very well.
No need for 3rd Party Libraries: With the popularity of third-party Date/Time
libraries like Joda Time, Java has to make its native Date/Time API comparable.
Now we can use the Java API instead of using 3rd party libraries.
381. What are the main differences between legacy Date/Time API in Java
and Date/Time API of Java 8?
In Java8, we have a new class Duration that provides the utility of computing
duration between two dates.
We can call the static method Duration.between(date1, date2) to get the time period
in hours, mins, days etc. between date1 and date2.
Java 8 has enhanced the Arrays class with methods that can run efficiently on multi
core machines.
This parallel set of methods provides parallel processing of Arrays that can run
Java code very fast on a multi core machine.
384. How does Java 8 solve Diamond problem of
Multiple Inheritance?
In Multiple Inheritance if a class extends more than one classes with two different
implementation of same method then it causes Diamond problem.
Consider following example to see problem and solution for
Diamond problem in Java 8:
In the above code, class ChildClass gives compile time error. Java Compiler cannot
decide which display method should it invoke in ChildClass.
To solve this problem, Java SE 8 has given the following remedy:
public interface A{
default void display() { //code goes here }
}
public interface B extends A{ }
public interface C extends A{ }
public class D implements B,C{
default void display() {
B.super.display();
}
}
Predicate is an anonymous function that accepts one argument and returns a result.
No, we have to always mark a default method in interface with default keyword.
No, it is not allowed to create a class that implements interfaces with same name
default methods.
In Multiple Inheritance a class can inherit behavior from more than one parent
classes.
Prior to Java 8, a class can implement multiple interfaces but extend only one class.
No, a static method of interface has to be invoked by using the name of the
interface.
392. How can you get the name of Parameter in Java by using
reflection?
Before using this feature, we need to turn on this feature in Java compiler.
Optional is a container object that may have a null or non-null value. If it has a
value then isPresent() method returns true.
It a value is present, we can call get() method to get the value. Else we will get
nothing.
In case, an Optional has null value, we can use orElseGet() method as fallback
mechanism. If we implement orElseGet() method, it will be invoked when the
value of Optional is null.
In Java 8 we can use Clock class to get the current time. Instead of using old
method System.currentTimeMillis(), we can create a Clock object and call millis()
method to get the current time in milliseconds.
We can also call instant() method on Clock object to get the current time in a
readable format.
Java 8 comes with a new command line tool jdeps that can help in analyzing the
package-level and class-level dependencies.
We can pass a jar file name or a class name as an argument to this tool. It will list
all the dependencies of that jar or class.
399. What are the new JVM arguments introduced by
Java 8?
StringJoiner is a new class in Java 8 that can be used to create a String. It can
construct a sequence of characters separated by a delimiter. It can also optionally
add a prefix and suffix to this sequence. We can use this sequence to get a String.
E.g.
The String "[One:Two:Three]" may be constructed as follows:
A lambda is like a method reference. It does not have a type of its own.
The target type of a lambda expression represents a type to which the expression
can be converted.
The lambda expression must have same parameter type as the parameter in the
function of the interface. It must also return a type compatible with the return type
of function.
An interface with a default method appears same as an Abstract class in Java. But
there are subtle differences between two.