Saturday, June 14, 2014

Java 8 : Frequently Asked Questions for Interviews - 6

How can a Lambda Expression be converted to Functional Interface? Example.
When a Lambda expression is executed 
  • an object of some class that implements the functional interface is created behind the scenes.
  • when the abstract method on this object is invoked, the body of lambda expression is executed.
PTR: The management of these classes and objects are totally dependent on implementation but its still very efficient than using traditional inner classes.
  • This conversion to interfaces is what makes Lambda Expressions so compelling as the syntax is short and simple, easier to read.
Example:
Array.sort(cars, (Car firstCar, Car secondCar) ->
        firstCar.getColor().compareTo(secondCar.getColor() 
    );

Can you Assign a lambda expression to a variable ( ofcourse of type Object) ?
No, Since Object is not a Functional Interface. It does not have any abstract method.

Can we declare function types such as (String, String) -> int, as used in other programming languages that support function literals?
No, CONVERSION TO FUNCTIONAL INTERFACE is the ONLY THING that one can do with Lambda Expressions in Java.

  • There are no function types added to Java language in Java 8.
  • There are only Lambda expressions that can be converted to functional interfaces.
  • This is natural for Java programmers as they know that certain interfaces e.g.. Comparator has a specific purpose. Its not just a method with given params and return type. So when you want to use Lamdba Expressions , its important to keep the purpose in perspective and have a specific functional interface for it.
For example: java.util.function has various generic functional interfaces. One such interface is BiFunction(T, U, R), describes functions with parameter types T and U and a return type R. We can use it as
BiFunction comp = (int first, int second) -> Integer.compare(first.length(), second.length())
However, that does not help with sorting, as Arrays.sort does not want a BiFunction. It wants a Comparator.

Are there any annotations introduced to help identify Functional Interfaces?
Yes, the annotation @FunctionalInterfaces can be used to tag any functional interface. It has two advantages 

  • The compiler will check the annotated entity to see if it has a single abstract interface or not.
  • The Javadoc page will include an statement that the annotated entity is a functional interfaces
However, the use of @FunctionalInterface is totally optional. Any interface having a single abstract method can be used a functional interface.

Does Checked exceptions matter in lambda Expressions? If yes, when and when not?
Yes, Checked exceptions matter when a lambda is converted to an instance of a functional interface. In case the body of lambda expression throws a checked exception, that exception needs to be declared in the abstract method of functional interface.
For example, Callable interface has an abstract call method that throws "Exception". However, the Run method in Runnable interface throws no exception of any sort.
Declaration :

public interface Callable<V> has abstract method V call throws Exception
Callable sleepDuringCall = () -> { 
    System.out.println("Calling"); 
    Thread.sleep(10000);} 
Thread.sleep (declared as public static void sleep(long miliseconds) throws InterruptedException) can throw checked exception.

Whereas,
public interface Runnable has abstract method void run()
Runnable sleepWhileRunning = () -> { 
    System.out.println("Sleeping during run"); 
    Thread.sleep(10); }
// will error out as Thread.sleep throws a checked exception.
However, you can catch the checked exception inside of the lambda body to keep the assignment legal.
Runnable sleepWhileRunning = () -> { 
    System.out.println("Sleeping during run"); 
    try{ 
        Thread.sleep(10); 
    }catch( InterrrupedException e){
        e.printStackTrace();
    }
}

No comments:

Post a Comment