Table of Contents
Java 8 Lambda Interview Questions
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
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");
3. What is the Difference in between Lambda Expression and Anonymous class?
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
Lambda Expression: With Lambda expression compiler will create only 1 class file as below.
So Java will create the new class file for each Anonymous class used.
4. Performance
Anonymous classes
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.
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/
good questions with nice explanation.
@Sourabh Thank You for sharing these types of tricky questions..