Before learning functional programming and why it got introduced in Java 8, let us learn the styles of programming in computer science.
Introduction
Paradigm is a way/method to do some task.
A programming paradigm is a style/way of programming and not referring to a specific language.
Types Of Programming Paradigm’s In Computer Science
- Procedural programming.
- Object-Oriented programming.
- Event-Driven programming.
- Functional programming.
- Logic programming.
- Aspect-Oriented programming, etc.
Out of all the above paradigms, we are going to learn about Functional programming and why it got introduced in Java 8. We will also discuss the topics where object-oriented falls short and how functional programming steps in and chimes.
In a nutshell, any software problem that a developer would like to solve can be implemented in both paradigms, the resultant solution looks different in coding styles.
Note: Java supports both Object-Oriented Programming and Functional Programming.
Java 8 introduced Java developers to functional programming with lambda expressions and the chain of methods that act on streams of data.
But are you aware that Java supports multi-paradigm?
Yes!! 🤩 Java supports multi-paradigm like (generic, object-oriented(class-based), functional, imperative, reflective, concurrent etc.
Functional Programming, Problems It Solves!
In computer science, functional programming is a programming paradigm where programs are constructed by applying and composing functions.
Functional and object-oriented programming are not opposites. They both will be used alongside.
You need to understand that object-oriented programming is an imperative style of programming, whereas functional programming is a declarative style of programming.
Functional programming is based on mathematical functions. Some of the popular functional programming languages include Python, Erlang, Clojure, etc.
Did you know all of the software problems or use-cases we solve using the object-oriented style of programming can also be solved using Functional programming?
Yes, 100% of all use-cases that can be solved using object-oriented can also be solved using functional programming, the best thing is we have to decide which one suits our use-case when designing the software before writing code.
Before Java 8, we developers knew Java is not a functional programming language and considered Object-Oriented programming for more than a decade. With Java 8 and later versions, we were introduced to functional programming in Java.
Let us try to understand more about what and why functional programming got introduced in Java, and what problems it is here to solve in Java where object-oriented falls short.
What is a Functional Programming?
With functional programming, we can bring awesome features into any programming language, like:
- Pure Functions.
- No Side Effects.
- Immutable data.
- Recursion
- First-class functions.
- Higher-order functions
- Lazy evaluation etc.
Important notes
Let us see some of the important points about functional programming.
Functional programming is programming with functions, the functions we refer to here are pure functions only.
Pure functions are representations of mathematical functions. This means functions just do one thing. They don’t depend on anything else but their arguments, and always produce the same result.
Functional interfaces in our application help us write lambda expressions, which is a way of writing functional programming in Java.
The other important note is, there are no loops in functional programming, all we have is implementing pure functions which have zero side effects.
The chain of methods next to .stream()
on collections data, is considered as doing functional programming in a way.
What Problems Functional Programming Solves In Java?
Before we jump directly into functional programming, example code snippets, etc, think about what problems it is here to solve in Java?
One of the biggest advantages of using functional programming is it reduces bugs in codes and improves reusability.
So, for pure functions, it always returns the same result/output given the same input arguments.
Functions are not called pure if they are changing the state of the object internally or externally.
The benefit of using a Functional Programming style in Java is that the side effect of a mutable input can be avoided.
Why has functional programming gained popularity?
In Object-Oriented programming, we bring the data(state) and behavior (methods) that operate on a single unit called objects.
In Functional programming, we think that data and methods are different, so we keep them separate. That’s the major difference between these two programming paradigms.
There are few places where object-oriented falls short or doesn’t provide much guidance, and in these precise areas, functional programming can step in and help.
Tabular structure differentiating both these paradigms
In the upcoming articles, I will talk more about Lambdas, Method References, Functional Interfaces, Stream API and more!
Top comments (1)
Awesome post ! Graphics are pretty much self explanatory