Java 8 Interview Questions And Answers

By | | Updated : 2021-05-30 | Viewed : 286 times

Java 8 Interview Questions And Answers

Many changes and features have been introducing in each version of Java. In Java 8, many changes, as well as features, have been found. So as a developer it is needed to know all these features. Here you can find a list of important interview Questions And Answers on Java 8.

1) what are new features introduced in java 8?

In Java 8 there are many more features have introduced. The main 15 features in Java 8 are

Lambda expressions

  • Functional interfaces

  • Default and static methods

  • Method references

  • Optional class

  • Stream API

  • Collectors class in Streams

  • ForEach() method in Streams

  • Nashorn JavaScript Engine

  • Parallel array sorting

  • Base64 Encode Decode

  • Type and Repeating Annotations

  • IO Enhancements

  • Concurrency Enhancements

  • JDBC Enhancements etc.

2) What are the advantages of using Java 8?

Listed below some of the important advantages of Java 8. Those are

  • 1. Compact code using lambda expressions

  • 2. Parallel operations using streams API

  • 3. Handling Null Pointer Exceptions using with Optional class is very easy.

  • 4. The functional Programming style of code is introduced and developed in such a way to encourage functional programming.

3) What is a lambda expression in Java 8?

Lambda expression is an initial step towards the encouragement of functional programming. Lambda Expression is a method/function body where a method/function body does not belong to any other Class. The generalized syntax of the lambda expression is

lambda parameter->(method body)

Please look into the below-given example of the Runnable interface.

Runnable interface
Runnable r1 = new Runnable() {
	@Override
	public void run() {
		System.out.println("Inside of Anonymous class");
		Employee employee 	= 	new Employee();
	}
};

The above example can be written as below

lambda expression for Runnable Interface
Runnable r2 = () -> {
    System.out.println("In side of Lambda Expression");
};

Please refer to basic-tutorial-for-lambda-expressions-in-java8 for more information about the lambda expressions in java 8.

4) What is the syntax of lambda expression

in Java, Lambda expressions has written with three types of parameters. Those are

Zero Parameterized lambda expression

The generalized syntax of Zero Parameterized is given below

Zero Parameterized lambda expression example
()->{System.out.println("hi")};

Single Parameterized lambda expression

The generalized syntax of Single Parameterized Lambda expression is given below

(param) -> {return param;}
Single Parameterized Lambda Expression example
(int param)->{System.out.println(param)}

Multi Parameterized lambda expression

The generalized syntax of Multi Parameterized Lambda expression is shown below.

(param1,param2) -> {return param1+param2;}
Multi Parameterized Lambda Expression example
(int param1,int param2)-> {return param1+param2};

5) What are functional interfaces in java 8?

A functional interface is an interface and it should contain only one abstract method. It may contain any number of default and static methods. The lambda expression is applicable for this single abstract method only in the functional interface.

Notice below given the example of a functional interface.

Example of a functional interface
public interface MathOps {
	public int operation(int x, int y);
}

So this abstract method is applicable for a lambda expression.

6) how to create the custom functional interface in java?

A custom functional interface is created using the keyword @FunctionalInterface. This keyword is used to represent the interface as a functional interface. Please find below an example for the custom functional interface is below.

Functional interface Example in Java 8
@FunctionalInterface
public interface Vehicle {
    	public default void getNoOfWheels {        
            System.out.println("The Vehicle will have four wheel");
        }
    	public default void getNoOfStreeingWheels {        
            System.out.println("The Vehicle will have One Steering wheel");
        }
 	public String getVehicleBrand();
}

In the example of a functional interface, you can see only one abstract method. The remaining methods are default and static methods.

7) How lambda expression and functional interfaces are related?

To provide Lambda expression, it needs a functional interface. The functional interface contains only one method where lambda expression is applicable for that abstract method of functional interface. You can see the example functional interface given above.

8) What are default methods?

default methods look like the normal methods in functional interfaces of Java 8. As the interface does not give more convenience in introducing a new method. The newly introduced method is required to implement in all the child classes even implementation is not required.

default method prefixed with default keyword. Notice below given example.

default method in functional Interface example
@FunctionalInterface
public interface Vehicle {
    	public default void getNoOfWheels() {        
            System.out.println("The Vehicle will have four wheel");
        }
    	public default void getNoOfStreeingWheels() {        
            System.out.println("The Vehicle will have One Steering wheel");
        }
 	public String getVehicleBrand();
}

getNoOfWheels() method is default method in Vehicle interface. So this method is not required in all the child classes.

9) What is optional in Java 8?

In Java 8, the new class is introduced. It is very useful in the handling of NullPointerException.

For example to avoid the null checks we can use an optional class.

Optional class example in Java 8
Optional<Integer> a = Optional.ofNullable(value);

Optional<Integer> b = Optional.of(value);

In the first example, the Optional class has allowed null wherein the second example does throw the NullPonterException.

10) What is the Method reference in java 8?

Method reference is a shorthanded code for a lambda expression. The syntax of Method reference is

class::method

To display the Product list in natural sorting order, the code will be as below.

To display the Product list in natural sorting order
Product product2 = new Product(2, "Redmi 9 Power", "Xiaomi ");
Product product3 = new Product(3, "Moto 5G", "Motorola");
Product product1 = new Product(1, "Galaxy F62", "Samsung");

ArrayList<Product> productList = new ArrayList<>();
productList.add(product1);
productList.add(product2);
productList.add(product3);

Collections.sort(productList, new Comparator<Product>() {
    public int compare(Product product1, Product product2) {
        return product1.compareTo(product2);
    }
});

So when you display the products the list appears in natural sorting order based on the product\'s id.

If we use lambda expression for writing the compare() then the code snippet is as below

using lambda expression for sorting product list
Collections.sort(productList, (product1, product2) -> product1.compareTo(product2));

Using with method reference for compare() method will be as below.

Using with method reference for compare()
Collections.sort(productList, Product::compareTo);
Leave A Reply