Java 8 Features
1) What is a functional interface in Java 8?
A functional interface is an interface that has only one abstract method. It can have any number of default and static methods. In Java 8, functional interfaces are used as the foundation for lambda expressions.
2) What are lambda expressions in Java 8?
Lambda expressions are a new feature in Java 8 that allows you to create anonymous functions. They provide a more concise and readable way to define a functional interface.
3) What is the purpose of the default keyword in Java 8?
The default keyword is used to define a default implementation of a method in an interface. This allows interfaces to have non-abstract methods, which can be overridden by implementing classes if needed.
4) What is the purpose of the static keyword in Java 8?
The static keyword is used to define a static method in an interface. This allows interfaces to have static methods, which can be called directly from the interface without needing an instance of the interface.
5) What are the new functional interfaces introduced in Java 8?
Java 8 introduced several new functional interfaces, such as Predicate
6) What is the purpose of the java.util.function package in Java 8?
The java.util.function package contains a number of functional interfaces that are used throughout the Java API in Java 8. These functional interfaces provide a standardized way to represent common functional constructs, such as predicates, consumers, and functions.
7) What is the Stream API in Java 8?
The Stream API is a new feature in Java 8 that allows you to work with collections of data in a more functional and efficient way. It provides a set of operations that can be applied to a stream of data, such as filtering, mapping, and reducing.
8) What is the difference between a stream and a collection in Java 8?
A collection is a data structure that stores a set of elements, such as a list or a set. A stream, on the other hand, is a sequence of elements that can be processed in parallel. The main difference between a stream and a collection is that the Stream API is designed to work with large collections of data and support parallel processing while collection API is designed to work with a small set of data.
9) What are the new date and time API in Java 8?
Java 8 introduced a new date and time API under the package java.time. The new API provides a more comprehensive and stable solution for working with dates and times. It includes classes for working with instants, durations, periods, and more.
10) What is the Optional class in Java 8?
The Optional class is a new container class that is introduced in Java 8. It is used to represent an optional value, which can be either present or absent. This class helps to prevent NullPointerExceptions and makes the code more readable.