Functional programming is a programming paradigm that is gaining popularity due to its emphasis on write code that is declarative, concise a and easy to reason about. In functional programming functions are treated as first-class citizens, meaning that they can be pass around as arguments, return as results and stored as variables.
Java incorporated many features of functional programming since the release of Java 8 , such lambda expression
, method references
, streams
and functional interfaces
-
Lambda Expressions
: Lambda expressions are a concise way of to define anonymous function, that can be used as parameters or return types in other methods- Java doesn’t have First-Class functions in a strict sense.
- Lambda expressions can be used as a replacement of first-class functions in many cases.
- A first-class function is a function that can be treated like any other data type, such as string or number. This means that you can pass a function to another function and store a function in a variable. In Java This is not possible because functions are not objects you can not pass them around like other data types
- Lambda expressions provide a way to define anonymous functions that can be used as parameters or return types in a method. They allow you to write code that is more concise and readable, specially when it comes to collections and streams .
- Lambda functions are not straight first-class functions in a sense but they provide much of the same functionality.
- Example of lambda expression in Java
- In this example the lambda expression
n -> n%2 == 0
to filter even numbers from a list
- In this example the lambda expression
List<Integer> numbers = List.of(1, 2, 3, 4, 5); /* using a lambda expression to filter even numbers */ List<Integer> evenNumbers = numbers.stream() .filter(n -> n % 2 == 0) .collect.toList();
-
Functional Interfaces
: Is an interface that contains only one abstract method. Functional interfaces are used extensively in Java’s functional programming features such as Lambda expressions and method references. -
Method References
Are a shorthand to refer to a method such as lambda expressions. They allow you to reuse existing methods as functions, which can simplify your code and make more readable.- final int x = 5;
-
Streams
Are a way to process collections of data in a functional way. Streams are created from collections or arrays and can be transmitted using terminal operations such asforEach
orreduce
-
Immutable Objects
Are objects that can not be modified after they are created. These can help simplify your code and make it more predictable as you don’t have worry about unexpected changes to the state of your program. -
Pure Functions
a function that returns the same output for given input and has no side effects. Pure functions are easy to reason about and can be parallelised, making them a powerful tool for concurrent programming. 1.int func(int a){ return a+1 } 2. Functions are not Pure whenever they refer to internal or external state change. e.g getters and setters in a class. 3. Let’s demo Non-pure toString()public class Person{ private String name; private int age; public void setName(String name){this.name = name} public String toString(){ return "name: "+this.name+" age "+age; } }
Pure toString()
public class Person{ private final String name; private final int age; public String toString(){ return "name: "+this.name+" age "+age; } }
In conclusion, functional programming is a powerful paradigm that can simplify your code and make it more predictable. Java’s support for features such as lambda expressions, method references and streams make it easier than ever to write functional code in Java. By incorporating these concepts into your code, you can write code that is more concise, maintainable and scalable .