Java 8 Lambda Expressions Introduction
- javastrokes
- Dec 7, 2016
- 2 min read
Why Do We Need Lambda Expressions?
Java was designed as an object-oriented programming language. An object-oriented programming (OOP) is the principal paradigm for software development. Everything in Java programming revolves around Objects (except some primitive types for simplicity).
In object-oriented programming, a function is called a method and it's a part of class, contains the logic. Methods are invoked on objects, which typically modify their states , thus producing side effects.

Functional programming, is based on the concept of functions, a block of code that accepts values, known as parameters, and the block of code is executed to compute a result. A function represents a functionality or operation. Functions do not modify data, including its input, thus producing no side-effects; for this reason, the order of the execution of functions does not matter in functional programming.
Suppose, if you want to use the particular functionality around the business components of your Java application, you need to create an object for a class, add a method to the object to represent the functionality or behavior, and pass the object around business components of your application. A “lambda expression” is a block of code and a higher-order function in functional programming, that you can pass around so it can be executed later, once or multiple times. A lambda expression in Java is is an unnamed block of code representing a functionality that can be passed around like data. lambda expressions in Java 8 represents an instance of a functional interface.
Lambda Expression Examples:
// Takes an simple float primitive type parameter and returns the parameter value incremented by 1
(float f ) -> f + 1
// Takes two double parameters and returns their sum
( double d1, double d2 ) -> d1 + d2
// Takes two long parameters and returns the maximum of the two
( long x, long y) -> { long max = x > y ? x : y;
return max;
}
Comments