Java 8 Flashcards
What are some of the new features released in Java 8?
Lambda expression, stream API, Date & Time API, Functional Interface, Optional
What is a lambda expression?
An anonymous function, like a method that does not need any access modifiers, name or return value declaration.
What are the three main parts of a Lambda expression?
Parameter list: Can be zero or more parameters.
Lambda arrow operator: it separates the list of parameters and the body of the Lambda.
Lambda expression body: the piece of code that we want to execute is written in the expression body.
What is the data type of a lambda expression?
The data type of a lambda expression is a functional interface.
What is a Functional Interface?
An interface that has exactly one abstract method. It can have default methods with implementation.
What are two popular functional interfaces?
java.lang.Runnable and java.util.Callable
What is a Single Abstract Method (SAM) interface in Java 8?
A functional interface is also known as a Single Abstract Method Interface, since it has only 1 abstract method.
How to create a functional interface?
Create an interface with exactly one abstract method. Another way is to mark an interface with @FunctionalInterface, this must follow the same rule.
What is the need of functional interfaces?
They are mainly used in lambda expressions, method reference and constructor references. In functional programming, code is treated as data, so lambda expressions can be used to pass a block of code to another method or object.
What are the main uses of Stream API?
It helps in using data in a declarative way. It makes good use of multi-core architectures. We can create a pipeline of data operations with Stream. It supports writing for code in functional programming styles.
What is the differences between Intermediate and Terminal Operations in Streams?
Intermediate operations are not evaluated until we chain it with a Terminal Operation. The output of Intermediate Operations is another Stream, the output of Terminal Operations is not a stream. We can chain multiple intermediate operations in a stream, but terminal operations cannot be chained multiple times. There can be multiple intermediate operations in a stream, there can only be one terminal operation in a stream.
What is a default method in an interface?
In Java 8, we can provide implementation of a method in an Interface and mark this method with Default keyword. This implementation of the method becomes default behaviour for any class implementing the interface.
What is the differences between Predicate, Supplier and Consumer in Java 8?
Predicate is an anonymous function that accepts one argument and returns a result.
Supplier is an anonymous function that accepts no argument and returns a result.
Consumer is an anonymous function that accepts one argument and returns no result.
What is Optional in Java 8?
Optional is a container object that may have a null or non-null value. If it has a value then isPresent() returns true. It is very useful in handling data that has null values.
What are the uses of Optional?
We can use Optional to avoid NullPointerException in an application. Optional performs Null check at compile time, so we do not get run time exception for a null value. Optional reduces the codebase pollution by removing unnecessary null checks.