Table of Contents
The basic objective of functional programming is to make code more concise, less complex, more predictable, and easier to test than the legacy coding style.
Functional programming deals with key concepts such as pure functions, immutable state, assignment less programming, etc.
Java is not purely a functional programming language but supports some functional programming features.
Functional programming is based on mathematical functions.
In recent years, Java has evolved to include more functional programming constructs, such as lambda expressions, functional interfaces, and streams.
Lambda expressions are anonymous functions that can be passed as arguments to methods or stored in variables. They allow for more concise and expressive code. For example, instead of creating a separate class for a comparator object, you can use a lambda expression to define the comparison logic inline.
Streams are a powerful API for processing collections of data. They allow for lazy evaluation and parallel execution, which can improve performance. Streams provide a declarative and concise way to express data transformations and allow for chaining multiple operations.
Java has been considered Object-oriented programming for over a decade. From Java 8, functional programming was introduced.
Functional programming is power-packed and brings some awesome features into Java programming.
- Pure functions.
- No side effects.
- Immutable data.
- First-class functions.
- Higher-order functions
- Lazy evaluation etc.
Functional programming deals with functions that are pure. Pure functions are representations of mathematical functions. This means that they do one thing. They don't depend on anything but their arguments and always produce the same result.
In the next chapters, we discuss functional interfaces that help us write lambda expressions, a way of writing functional programming in Java.
The other important note about functional programming is there are no loops. All we have is implementing pure functions which have zero side effects.
The chain of methods next to
.stream()are considered as doing functional programming in a way.
Differences between OOP and FP
Object-oriented programming (OOP) and functional programming (FP) are popular programming paradigms with different approaches to structuring code and solving problems.
In summary, while Java is not a purely functional programming language, it has incorporated many functional programming concepts that make it easier to write functional-style code.
Gopi Gorantala Newsletter
Join the newsletter to receive the latest updates in your inbox.