Java 8 Flashcards
What are the new features introduced in JAVA 8?
There are dozens of features added to Java 8, the most significant ones are mentioned below −
- Lambda expression − Adds functional processing capability to Java.
- Method references − Referencing functions by their names instead of invoking them directly. Using functions as parameter.
- Default method − Interface to have default method implementation.
- New tools − New compiler tools and utilities are added like ‘jdeps’ to figure out dependencies.
- Stream API − New stream API to facilitate pipeline processing.
- Date Time API − Improved date time API.
- Optional − Emphasis on best practices to handle null values properly.
- Nashorn, JavaScript Engine − A Java-based engine to execute JavaScript code.
- Along with these new features, lots of feature enhancements are done under-the-hood, at both compiler and JVM level.
How will you sort a list of string using Java 8 lambda expression?
Following code sorts a list of string using Java 8 lambda expression:
//sort using java 8 private void sortUsingJava8(List names) { Collections.sort(names, (s1, s2) -> s1.compareTo(s2)); }
What are the characteristics of a Java 8 lambda expression?
A lambda expression is characterized by the following syntax -
parameter −> expression body
Following are the important characteristics of a lambda expression −
- Optional type declaration − No need to declare the type of a parameter. The compiler can inference the same from the value of the parameter.
- Optional parenthesis around parameter − No need to declare a single parameter in parenthesis. For multiple parameters, parentheses are required.
- Optional curly braces − No need to use curly braces in expression body if the body contains a single statement.
- Optional return keyword − The compiler automatically returns the value if the body has a single expression to return the value. Curly braces are required to indicate that expression returns a value.
Why lambda expression is to be used?
Lambda expressions are used primarily to define inline implementation of a functional interface, i.e., an interface with a single method only. In the above example, we’ve used various types of lambda expressions to define the operation method of MathOperation interface. Then we have defined the implementation of sayMessage of GreetingService.
Lambda expression eliminates the need for an anonymous class and gives a very simple yet powerful functional programming capability to Java.
What kind of variable you can access in a lambda expression?
Using lambda expression, you can refer to final variable or effectively final variable (which is assigned only once). Lambda expression throws a compilation error if a variable is assigned a value the second time.
What are method references?
Method references help to point to methods by their names. A method reference is described using :: (double colon) symbol. A method reference can be used to point the following types of methods −
- Static methods
- Instance methods
- Constructors using new operator (TreeSet::new)
Explain the System.out::println expression.
System.out::println method is a static method reference to println method of out object of System class.
What are functional interfaces?
Functional interfaces have a single functionality to exhibit. For example, a Comparable interface with a single method ‘compareTo’ is used for comparison purpose. Java 8 has defined a lot of functional interfaces to be used extensively in lambda expressions.
What is the purpose of BiConsumer functional interface?
It represents a function that accepts two arguments and produces a result.
What is the purpose of BinaryOperator functional interface?
It represents an operation upon two operands of the same type, producing a result of the same type as the operands.
What is the purpose of BiPredicate functional interface?
It represents a predicate (Boolean-valued function) of two arguments.
What is the purpose of BooleanSupplier functional interface?
It represents a supplier of Boolean-valued results.
What is the purpose of Consumer functional interface?
It represents an operation that accepts a single input argument and returns no result.
What is the purpose of DoubleBinaryOperator functional interface?
It represents an operation upon two double-valued operands and producing a double-valued result.
What is the purpose of DoubleConsumer functional interface?
It represents an operation that accepts a single double-valued argument and returns no result.
What is the purpose of DoubleFunction functional interface?
It represents a function that accepts a double-valued argument and produces a result.
What are default methods?
With java 8, an interface can have default implementation of a function in interfaces.
What are static default methods?
An interface can also have static helper methods from Java 8 onwards.
public interface vehicle {
default void print() {
System.out.println(“I am a vehicle!”);
}
static void blowHorn() {
System.out.println(“Blowing horn!!!”);
}
}
How will you call a default method of an interface in a class?
Using super keyword along with interface name.
interface Vehicle { default void print() { System.out.println("I am a vehicle!"); } } class Car implements Vehicle { public void print() { Vehicle.super.print(); } }