What is functional programming, and programming paradigms in Java

Functional programming in Java
Functional programming in Java

Before learning functional programming and why it was introduced in Java 8, let us understand the programming styles in computer science.

Functional programming

A paradigm is a way/method to do some task.

A programming paradigm is a style/way of programming and does not refer to a specific language.

Types of programming paradigms in computer science

  1. Procedural programming.
  2. Object-oriented programming.
  3. Event-driven programming.
  4. Functional programming.
  5. Logic programming.
  6. Aspect Oriented programming, etc.
Programming Paradigms

Out of all the above paradigms, we will 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.

Any software problem can be implemented in both paradigms, and 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.

Java supported programming paradigms

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, whereas functional programming is a declarative style.

Functional programming is based on mathematical functions. Some 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 programming style 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 to decide which one suits our use case when designing the software before writing code.

Before Java 8, we developers knew Java was not a functional programming language and considered Object-Oriented programming for over 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 functional programming?

With functional programming, we can bring excellent features into any programming language, like:

  1. Pure functions.
  2. No side effects.
  3. Immutable data.
  4. Recursion.
  5. First-class functions.
  6. Higher order functions.
  7. Lazy evaluation, etc.
Functional Programming

Important notes

Let us see some of the crucial points about functional programming.

  1. Functional programming is programming with functions. The functions we refer to here are pure only.
  2. 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.
  3. Functional interfaces in our application help us write lambda expressions, a way of writing functional programming in Java.
  4. The other important note is that there are no loops in functional programming. All we have is implementing pure functions which have zero side effects.
  5. The chain of methods next to .stream() collections data is considered as doing functional programming in a way.

What problems does functional programming solve in Java?

Before we jump directly into functional programming, for example, code snippets, etc., think about what problems it is here to solve in Java.

One of the most significant advantages of using functional programming is that it reduces code bugs 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 change the object's state 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?

Object-oriented programming brings the data(state) and behaviour (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 significant 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

HTML Table

Functional programming Object-oriented programming
1. Emphasizes on evaluation of functions. 1. Based on concepts of objects.
2. Statements executing in here can be of any order. 2. Statements executed here are executed in a particular order.
3. Supports parallel programming. 3. This does not support parallel programming.
4. Follows logic without describing the control flow. 4. Follows statements that actively change the state of the program.
5. This focuses on WHAT. 5. This focuses on HOW rather than WHAT.
6. Uses Immutable data. 6. Uses Mutable data.
7. In this style, recursion is used for iterative data. 7. Here, loops are used for iterative data.

In the upcoming articles, I will discuss Lambdas, Method References, Functional Interfaces, Stream API and more!