Skip to content

Introduction To Functional Programming

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.

FP 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

Gopi Gorantala Twitter

Gopi is a highly experienced Full Stack developer with a deep understanding of Java, Microservices, and React. He worked in India & Europe for startups, the EU government, and tech giants.

Comments


Related Posts

Members Public

How To Write Lambda Expressions

This lesson introduces the basic lambda expression structure with tips to write efficient code.

Members Public

What Are Lambda Expressions?

This is an introductory lesson on lambda expressions. You will learn about the lambda operator, expression, syntaxes and more!

Members Public

Power Of Two (Exercise Problem)

This is an exercise problem for your practice. Try to come up with an approach and solve it by yourself. Good Luck!