Skip to content

How Functional Programming is Different From OOP?

In this lesson, you will learn about functional programming and the fundamental differences between object-oriented and functional programming.

Gopi Gorantala
Gopi Gorantala
2 min read

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.

Introduction

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.

Features

Functional programming is power-packed and brings some awesome features into Java programming.

  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 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.

Conclusion

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.

Java Streams APIJava

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Differences Between JDK, JRE, and JVM?

Short answer JDK, JRE, and JVM are essential components of the Java platform, each serving a distinct purpose. Here are the key differences between them: 1. JDK (Java Development Kit): The JDK is used by developers to write, compile, and debug Java code. 2. JRE (Java Runtime Environment): End-users use

Members Public

Difference Between String and char[] in Java

Short answer Strings String is an object with many helpful methods. String class in Java's standard library is designed to handle text as a sequence of characters. A string of characters (string object) is non-modifiable or immutable in Java. Once you've created it, you cannot modify

Members Public

What is an Object class in Java?

Short answer Object class is the super class of every class you can create. In Java, a class extends another class using the keyword extends. If you don't have any other class to extend, that's fine. The compiler will make your class extend the Object class.