Java 8 Interview Questions and Answers: Java Guides Ramesh Fadatare
Java 8 Interview Questions and Answers: Java Guides Ramesh Fadatare
I would like to share my experience with Java 8 interview questions. Let's list all commonly
asked interview questions and answers regarding Java 8 features and enhancement.
Along with these new features, lots of feature enhancements are done under-the-hood, at
both compiler and JVM level.
The below diagram shows all the Java 8 features and enhancements.
Blog Site: Java Guides Author: Ramesh Fadatare
Lambda expressions introduce functional style processing in Java and facilitate the writing
of compact and easy-to-read code.
Because of this, lambda expressions are a natural replacement for anonymous classes as
method arguments. One of their main uses is to define inline implementations of
functional interfaces.
Read more in-detail about lambda expressions at Java 8 Lambda Expressions
import java.util.function.Predicate;
public class PredicateInterfaceExample {
public static void main(String[] args)
{
// Creating predicate
Predicate<Integer> lesserthan = i -> (i < 18);
Blog Site: Java Guides Author: Ramesh Fadatare
Output
true
@FunctionalInterface
interface Sayable{
void say(String msg); // abstract method
}
For more on functional interfaces, see the article at Java 8 Functional Interfaces with
Examples.
can become:
Object::toString();
A method reference can be identified by a double colon separating a class or object name
and the name of the method. It has different variations such as constructor reference:
String::new;
String::valueOf;
str::toString;
Blog Site: Java Guides Author: Ramesh Fadatare
String::toString;
You can read a detailed description of method references with full examples at Java 8
Method References.
ContainingClass::staticMethodName
containingObject::instanceMethodName
ContainingType::methodName
ClassName::new
You can read a detailed description of method references with full examples at Java 8
Method References.
Blog Site: Java Guides Author: Ramesh Fadatare
And yet another important distinction from collections is that streams are inherently lazily
loaded and processed.
Read more about streams at Java 8 Stream APIs with Examples.
roster
Blog Site: Java Guides Author: Ramesh Fadatare
.stream()
.filter(e -> e.getGender() == Person.Sex.MALE)
.forEach(e -> System.out.println(e.getName()));
Read more about Optional class with examples at Java 8 Optional Class with Examples.
.map(Object::toString)
.collect(Collectors.joining(", "));
Collectors.summingInt(Employee::getSalary)));
Read more about Collectors class at Java 8 Collectors Class with Examples.
Read more about StringJoiner class at Java 8 StringJoiner Class with Examples.
String speedUp();
String slowDown();
Usually, when a new abstract method is added to an interface, all implementing classes will
break until they implement the new abstract method. In Java 8, this problem has been
solved by the use of default method.
For example, Collection interface does not have forEach method declaration. Thus, adding
such method would simply break the whole collections API.
Java 8 introduces default method so that Collection interface can have a default
implementation of forEach method without requiring the classes implementing this
interface to implement the same.
Read more about Default Methods with examples at Java 8 Static and Default Methods
in Interface.
•In a typical design based on abstractions, where an interface has one or multiple
implementations, if one or more methods are added to the interface, all the
implementations will be forced to implement them too. Otherwise, the design will
just break down so default interface methods are an efficient way to deal with this
issue. They allow us to add new methods to an interface that are automatically
available in the implementations. Thus, there’s no need to modify the implementing
classes. In this way, backward compatibility is neatly preserved without having to
refactor the implementers.
•The default method is used to define a method with a default implementation. You
can override the default method also to provide the more specific implementation
for the method.
Blog Site: Java Guides Author: Ramesh Fadatare
Read more about Default Methods with examples at Java 8 Static and Default Methods
in Interface.
String speedUp();
String slowDown();
interface Vehicle {
default void print() {
System.out.println("I am a vehicle!");
}
}
class Car implements Vehicle {
public void print() {
Vehicle.super.print();
}
}
interface Vehicle {
static void blowHorn() {
System.out.println("Blowing horn!!!");
}
}
class Car implements Vehicle {
public void print() {
Vehicle.blowHorn();
}
}
Blog Site: Java Guides Author: Ramesh Fadatare
This above diagram shows Diamond Problem. To avoid this problem, Java 7 and Earlier
versions does not support methods implementation in interface and also doesn’t support
Multiple Inheritance. Java 8 has introduced new feature: Default methods to support
Multiple Inheritance with some limitations.
Sample Java SE 8 Code to show this Diamond Problem:
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{ }
Blog Site: Java Guides Author: Ramesh Fadatare
In the above code snippet, class D gives compile time errors because Java Compiler will get
bit confusion about which display() has to provide in class D. Class D
inherits display() method from both interfaces B and C. 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{
void display() {
B.super.display();
}
}
1.Poor API design (for example, months start with 1 and days start with 0)
Blog Site: Java Guides Author: Ramesh Fadatare
The new Date and Time API was introduced in Java SE 8 with the following solutions:
1.The improved API design clearly separates the human-readable date time and
machine time.
2.The new Date and Time API makes all the classes immutable, which is suitable for
the multithreaded environment.
3.There is more clarity in the API design level. The methods are clearly defined and
perform the same action in all classes.
4.The API defines separate classes for Date, Time, DateTime, Timestamp, TimeZone,
and so on. The new Date and Time API works with the ISO-8601 calendar. However,
you can use the API with non-ISO calendars as well.