0% found this document useful (0 votes)
45 views

Functional Programming in Java

Functional programming is a declarative programming paradigm where the basic unit of computation is a function. Functions are treated like mathematical functions that map inputs to outputs without altering program state. In functional programming, software is developed by evaluating functions rather than executing a sequence of statements. Some key aspects include functions being isolated, independent units that depend only on their arguments and do not modify global variables. This makes the code more concise, readable and predictable compared to imperative programming.

Uploaded by

Sanket
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views

Functional Programming in Java

Functional programming is a declarative programming paradigm where the basic unit of computation is a function. Functions are treated like mathematical functions that map inputs to outputs without altering program state. In functional programming, software is developed by evaluating functions rather than executing a sequence of statements. Some key aspects include functions being isolated, independent units that depend only on their arguments and do not modify global variables. This makes the code more concise, readable and predictable compared to imperative programming.

Uploaded by

Sanket
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Functional Programming in Java

Functional programming is a subset of the Declarative programming paradigm.


Functional programming technique makes our code more concise, readable, and
predictable.
Introduction to Programming Paradigm

Programming Paradigm is a way or style of programming. The programming paradigm can


be classified into two types:

 Imperative programming paradigm


 Declarative programming paradigm

Imperative Programming Paradigm

Imperative programming paradigm consists of a sequence of statements that changes the


program's state until the target result is achieved. The main focus is on how to achieve the
goal. It consists of three main programming approaches:

 Procedural programming
 Object Oriented programming
 Parallel Processing Approach

public class Main {

public static void main(String[] args) {

int sum = 0;

for (int i = 1; i <= 5; i ++) {

sum += i;

System.out.println(sum);

Declarative Programming Paradigm

Declarative programming is a paradigm in which we define what needs to be accomplished


without defining how it has to be implemented. In the declarative programming paradigm, for
the same input arguments, the program produces the same result. The order of execution of
statements is not important in the declarative programming paradigm. It consists of three
main programming approaches:

 Logic programming paradigm


 Functional programming
 Database processing approach

import java.util.Arrays;

public class Main {

public static void main(String[] args) {

int[] array = new int[] {1, 2, 3, 4, 5};

int[] evenArray = Arrays.stream(array)

.filter(a -> a % 2 == 0)

.toArray();

System.out.println(Arrays.toString(evenArray));

In this example, the input array is converted to stream. The filter() method is invoked for
each value in the stream and it is evaluated with the expression a -> a % 2 == 0.

filter(1) => false


filter(2) => true
filter(3) => false
filter(4) => true
filter(5) => false

The filter() method passes the value to the next operation i.e. toArray() only if the expression
inside it evaluates to true. So, in this case, only 2 and 4 are passed to toArray() operation, and
hence the output is [2, 4].

Here, we didn't define the steps to filter the even numbers. Instead, we told the compiler what
we want via the expression filter(a -> a % 2 == 0).

What is Functional Programming in Java?


Functional programming is a paradigm where the basic unit of computation is a
function.
Here functions are not the methods we write in programming. Methods are
procedures where we write a sequence of instructions to tell the computer what
to do. In functional programming, functions are treated like mathematical
functions where we map inputs to outputs to produce a result. Eg.

f(x)=3x.
In functional programming, the software is developed around the evaluation of
functions.
The functions are isolated and independent, and they depend only on the
arguments passed to them and do not alter the program's state like modifying a
global variable.

You might also like