Java Lambda Expressions Basics

   2020-01-02       69        Technology
Rating :
A lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body.

What is a Lambda Expression?

A lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body.

The following are examples of Java lambda expressions:

n -> n % 2 != 0;
(char c) -> c == 'y';
(x, y) -> x + y;
(int a, int b) -> a * a + b * b;
() -> 42
() -> { return 3.14 };
(String s) -> { System.out.println(s); };
() -> { System.out.println("Hello World!"); };

Interpretation of Examples

  1. Given a number n returns a boolean indicating if it is odd.
  2. Given a character c returns a boolean indicating if it is equal to â€˜y’.
  3. Given two numbers x and y returns another number with their summation.
  4. Given two integers a and b returns another integer with the sum of their squares.
  5. Given no parameters returns the integer 42.
  6. Given no parameters returns the double 3.14.
  7. Given a string s prints the string to the main output and returns void.
  8. Give no parameters prints Hello World! to the main output and returns void.

Take your career to new heights of success with an java online training

The Parameters

  • A lambda expression can receive zero, one or more parameters.
  • The type of the parameters can be explicitly declared or it can be inferred from the context.
  • Parameters are enclosed in parentheses and separated by commas.
  • Empty parentheses are used to represent an empty set of parameters.
  • When there is a single parameter, if its type is inferred, it is not mandatory to use parentheses.

The Body

  • The body of the lambda expression can contain zero, one or more statements.
  • When there is a single statement curly brackets are not mandatory and the return type o"
"

Author Signature :


A lambda expression represents an anonymous function. It comprises of a set of parameters, a lambda operator (->) and a function body.


Random Articles

Understanding revenue cycle management
Understanding revenue cycle management..
   2019-10-28       58        Business
Know How Approaching a Trusted Old Age Home Can Be Beneficial for You
Know How Approaching a Trusted Old Age Home C..
   2019-10-11       46        Services
How to remove acne and acne scars naturally for oily skin?
How to remove acne and acne scars naturally f..
   2019-06-03       97        Health
Scale Up the Repatriation Requirement with King Air Ambulance Jamshedpur
Scale Up the Repatriation Requirement with Ki..
   2021-09-16       11        Health
best golf simulator, buy golf simulator
best golf simulator, buy golf simulator..
   2020-03-14       70        Business
Top App Developers in Canada
Top App Developers in Canada..
   2021-02-11       24        Computer
Top-trending pieces have just dropped with our new arrivals!
Top-trending pieces have just dropped with ou..
   2020-03-05       57        Shopping
Cloud Computing and Networking
Cloud Computing and Networking..
   2021-12-06       16        Communication
What is Everest Helicopter Tour around Nepal ?
What is Everest Helicopter Tour around Nepal ..
   2019-08-01       103        Services
Best electric stand whisk
Best electric stand whisk..
   2020-05-30       50        Electronics
Australia Skillselect Updates for May 2019
Australia Skillselect Updates for May 2019..
   2019-07-17       125        Education
Mobile App Development Company for iOS & Android in Dubai
Mobile App Development Company for iOS & Andr..
   2020-02-07       60        Business
Backpage Humboldt County || Site Similar To Backpage
Backpage Humboldt County || Site Similar To B..
   2018-10-30       199        Business
ABOUT US

Some quick example text to build on the card title and make up the bulk of the card's content Some quick example text to build on the card title and make up the bulk of the card's content Some quick example text to build on the card title and make up the bulk of the card's content.

TOP AUTHORS
Thomas Shaw
thomasshaw9688@gmail.com
Total Posts : 13511
zero promosi
zero1601@gmail.com
Total Posts : 2088