Java8 Interview Questions

What are the new Java8 Features?

  •  Lambda Expression
  • Interface Unlocking using the default method.
  • Interface Static Method.
  • Functional Interface
  • Functions Library
  • Streams
  • ParallelStream 
  • New Date and Time API influences by JODA Time.
  • Nashorn Javascript Engine

What is Functional Interface?

An Interface having only a single abstract method is called as functional Interface. So only one unimplemented method but default and static method can be there.
Example of a functional class.

public interface FunctionalTest {
public void functionalAbstract();}

A functional interface can be implemented by Lambda Expression in java as below example, we need not write the implements Interface also in class header.

public class Test {
public static void main(String[] args) {
FunctionalTest testing = () ->{
System.out.println("Lambda Implementing Functional");
};}}

What are the static methods in java8 interface and what is the use of it?

From java8 onwards static methods are allowed in an interface. Static methods declared in the interface will belong interface. It can not be instantiated with the implemented class object.
Through the Static method, we can avoid using general utility classes (like Collections) and calling static methods through their proper interface.
It is secure as the implementing class can not override it.

What are the default methods in java?

Default methods are methods that need not be overridden by all implementing classes. Say you have an interface which is getting implemented by many classes, now you want to add a new method which is required by few classes and it is not possible to override the method in all classes. Hence if the method is default then it need not be overridden by all classes.

public interface FunctionalTest {

public void functionalAbstract();

public default void defaultTest() {
System.out.println("test");
}
}

What is Lambda?

Lambdas are anonymous functions. Lambda expression is used by using ->.


what is completable futures in java8?

They are Futures that also allow you to string tasks together in a chain. You can use them to tell some worker thread to "go do some task X, and when you're done, go do this other thing using the result of X"

In a lambda expression can we access a local variable in the method?

Yes we can access the local variable in lambda but it should be final and private, lambdas can not make changes to the variable.

What is a predicate in Java8

The predicate is a functional interface, and its mostly used in method reference and assignment target. Below is an example for a predicate to list of students whose score is above 7.
public static Predicate isAbove7() {
return p -> p.getScore > 7;
}

What are the changes in Strategy Pattern implementation in java8 with respect to lambda?

Because Lambda expression uses a Functional interface, hence instead of creating concrete classes for each behavior method we can implement them directly in the main class as below example.

  
  package developerasks.design.startegy.cameraApp;

public interface Share {
	public void shraeBy();

}

public class BasicCamera extends PhoneCameraApp {
	
	public BasicCamera() {
	 
	 //Before Java8 Create a concrete class for each implementation 
	 //Here we are using a concretea class ShareByText and then used.
		share = new ShareByText();
		
	//Java8 We need not create a concrete class as it gets implemented by //lambda as below	
		List sharing = Arrays.asList(
				() -> {
					System.out.println("Share By Text");
				}	);
	}	
	}

  
  
Previous
Next Post »

Pages