In this exercise, we have given the Main Java 8 interview Questions and their answers with Code Models and detail Clarification:
Q-1) List down Java 8 new features ?
Answer: New features that are introduced in Java 8 are listed below:
- Lambda Expressions
- Functional Interface
- Stream API
- Method References
- Optional Class
- Default methods
- Date API
Q-2) What is an optional class?
Answer: Optional class is a special wrapper class introduced in Java 8 which is used to avoid NullPointerExceptions. This final class is present under java.util package. NullPointerExceptions occurs when we fail to perform the Null checks.
Q-3) What are Functional Interfaces?
Answer: Functional Interface is an interface that has only one abstract method. The implementation of these interfaces can be provided using a Lambda Expression which means that to use the Lambda Expression, you need to create a new functional interface or you can use the predefined functional interface of Java 8.
“@FunctionalInterface” Annotation is used for creating new functional interface.
If we use @FunctionalInterface annotation and having more then one abstract method then it will show compile error .
Q-4) What are the default methods?
Answer: Default methods are the methods of the Interface which has a body. These methods, as the name suggests, use the default keywords. Default method mainly used for “Backward Compatibility” which means if JDK modifies any Interface (without default method) then the classes which implement this Interface will break.
But if you add the default method in an Interface then you will be able to provide the default implementation. This won’t affect the implementing classes.
Q-5) What are the main features of the Lambda Function?
Answer: Main features of the Lambda Function are as follows:
- A method that is defined as Lambda Expression can be passed as a parameter to another method.
- A method can exist standalone without belonging to a class.
- There is no need to declare the parameter type because the compiler can fetch the type from the parameter’s value.
- There is no need to have parenthesis when we use a single parameter but can use parentheses when using multiple parameters .
- If the body of expression has a single statement then there is no need to include curly braces.
Q-6) What was wrong with the date and time before java8?
Answer: below are the drawbacks of the date and time before java8:
- Java.util.Date is mutable and is not thread-safe whereas the new Java 8 Date and Time API are thread-safe.
- Java 8 Date and Time API meets the ISO standards whereas the before java8 date and time were poorly designed.
- It has introduced several API classes for a date like LocalDate, LocalTime, LocalDateTime, etc.
- Java 8 Date and Time works faster than the old regime of date and time.
Q-7) What is a SAM Interface?
Answer: In Java 8 it introduced the concept of Functional Interface that can have only one abstract method. Since these Interfaces specify only one abstract method, they are sometimes called as SAM Interfaces. SAM stands for “Single Abstract Method”.
Q-8) What is Method Reference?
Answer: Method Reference is one of the new feature introduced in java8. This is used to refer to the method of functional interface. It can be used to replace Lambda Expression while referring to a method.
For Example: If the Lambda Expression looks like
num -> System.out.println(num)
Then the corresponding Method Reference would be,