GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Java/Lambda at master · shivgy/Java

Expand file tree

/

Lambda

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

Lambda

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
Readme.md
Readme.md
 
 

Readme.md

Outline

Lambda Expression

  • A Lambda expression is a function which can be created without belonging to any class.
  • It can be passed around as an object and executed on demand.
  • It provides clear and concise way to represent one method interface using an expression.
  • Before Lambda Expression, anonymous inner class was the only option to implement the method.

Without Lambda Expression

Note: The interface is not implemented in the main class(LambdaExpressionExample). Rather, while instantiating, the unimplemented method is defined there itself. If the interface contains more than one method all the methods must be defined in there.

This will be cumbersome so we implement the interface calss.

interface Drawable{
  public void draw();
}

public LambdaExpressionExample{
  public static void main(String[] args){
    int width = 10;
    Drawable d = new Drawable(){
       public void draw(){
        System.out.println("Drawing " + width);
       }
    };
    d.draw();
  }
}

Using Lambda Expression

Note: Only single method is present in the interface where we use Lambda Expression.

interface Drawable{
  public void draw();
}

public class LambdaExpressionExample{
  public static void main(String[] args){
    int width = 10;
    Drawable d = ()->{
      public void draw(){
        System.out.println("Drawing " + width);
      }
    };
  }
}
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.