Java 8 Lambda Interview Questions

Java 8 Lambda Interview Questions

Java 8 Lambda Interview Questions Only Fullstack

1. What is Lambda Expression in Java 8?

Lambda expression facilitates the functional programming and simplifies the development. Lambda expression is a shorter way of writing an implementation of a single abstract method interface. Lambda expressions are used to create anonymous functions. Syntax: (parameters) -> expression or (parameters) -> { statements; } Java 8 provide support for lambda expressions only with functional interfaces. Lets see how to use lambda to remove the boilerplate code. We will implement the Runnable interface with anonymous class and with lambda expression.

private static void simpleConceptWithRunnable() {
 Runnable runnable = new Runnable() { // boilerplate code which needs to be written everytime
  @Override
  public void run() {
   System.out.println("Inside annonymos inner class");
  }
 };
 
 Runnable runnable2 = () -> { // Lambda expression
         System.out.println("Inside lambda expression");
    };
 
 Thread thread = new Thread(runnable);
 thread.start();
 
 thread = new Thread(runnable2);
 thread.start();
}

We wrote a boilerplate code of creating an anonymous class and override the run method. Lambda takes care of this things and gives us a simple syntax to create a anonymous class. Lambdas are more than anonymous classes.

2. How to use Lambda Expression with Parameters

1. No Parameters – 
Runnable runnable2 = () -> System.out.println("Inside lambda expression");


2.Single Parameter –

interface SingleParam {

public void print(String param);

}

SingleParam singleParam = (param) -> {System.out.printf("Hello %s",param);};

singleParam.print("saurabh");


3. Multiple parameters –

interface MultipleParam {

public void print(String param1, String param2);

}

MultipleParam multipleParam = (param1, param2) ->
                    System.out.printf("param1 : %s, param2: %sn", param1, param2);

multipleParams.print("OnlyFullstack", "Development");
U1

3. What is the Difference in between Lambda Expression and Anonymous class?

1. Syntax
Anonymous class:

package com.onlyfullstack;
public class LambdaVsAnonymousClass {

 public static void main(String[] args) {
  Runnable runnable = new  Runnable() {
   @Override
   public void run() {
    System.out.println("Anonymous class");
   }
  };
  
  Thread thread = new Thread(runnable);
  thread.start();
 }
}

Lambda:

package com.onlyfullstack;

public class LambdaVsAnonymousClass {

 public static void main(String[] args) {
  Runnable runnable = () -> System.out.println("Lambda Expression");
  
  Thread thread = new Thread(runnable);
  thread.start();
 }
}

2. Implementation
Anonymous class can be used to implement any interface with any number of abstract methods.
Lambda Expression will only work with SAM(Single Abstract Method) types. That is interfaces with only a single abstract method which is also called as Functional Interface. It would fail as soon as your interface contains more than 1 abstract method.

3. Compilation
Anonymous class : Java creates two class files for LambdaVsAnonymousClass java file as
LambdaVsAnonymousClass.class     – contains the main program
LambdaVsAnonymousClass$1.class – contains an anonymous class

Untitled

Lambda Expression: With Lambda expression compiler will create only 1 class file as below.

Untitled1

So Java will create the new class file for each Anonymous class used.

4. Performance
Anonymous classes

Anonymous classes are processed by the compiler as a new subtype for the given class or interface so there will be generated a new class file for each anonymous class used. When the application is launched each class which is created for the Anonymous class will be loaded and verified. This process is quite a time consuming when you have a large number of anonymous class.

Lambda expressions

Instead of generating direct bytecode for lambda (like proposed anonymous class syntactic sugar approach), compiler declares a recipe (via invokeDynamic instructions) and delegates the real construction approach to runtime.

So Lambda expressions are faster than the Anonymous classes as they are only executed when they are called.

4. How Lambda Expression Internally Works

https://www.onlyfullstack.com/lambda-expression-internal-working/

5. What lambdas are compiled to?

https://www.onlyfullstack.com/lambda-expression-internal-working/

1 Comment

Comments are closed.