From the course: Java: Lambdas and Streams

What is functional programming? - Java Tutorial

From the course: Java: Lambdas and Streams

What is functional programming?

- [Instructor] To understand lambdas and streams, it's useful to understand first what functional programming is. You've probably heard Java described as an object-oriented programming language. And if you've done any coding with Java before you'll be familiar with the concepts of classes and objects. In fact, classes and objects were probably one of the first things you learned when you picked up Java. So in Java, you might have a class called car, and this might have a field called gas level and a field called mileage and a method called drive. So then you would create objects of type car, and then you might call the drive method on the car objects. This might then change the state of the object. So the fuel level might go down and the mileage might go up. So that's how you think of object-oriented programming. And that concept should hopefully be quite familiar. Functional programming is a different way of thinking about coding. It's quite different to object-oriented programming, but once you get used to it, it can be very powerful and let you write clean code without using lots of boiler plates. In functional programming, you forget about classes and objects and all of that and you just focus on functions. Functions in functional programming give you the same output every time you give the same input. So let's say you have a function called double the number. If you give it the input three, the output will always be six. If you give it 140, the output will always be 280. So this is quite straightforward with things like number operations. As another example, let's say you have a function called filter to filter out numbers we don't like. Say for some reason we don't like the number four, so we tell the function to filter out fours. If we parse on a list of numbers, e.g., one, two, three, four, five, the outputs will be one, two, three, five. But you might want to do some more sophisticated filtering, like filtering out odd numbers. So there might be a function called isOddNumber to work out if a number is even or odd. And instead of parsing in the number four, you can parse in that function as an argument to the filter function. So then if the input is one, two, three, four, five, the output will be two, four, because it's filtered out the odd numbers using another function. So functions can take functions as input. And in some cases, the output might be a function as well. So you might set a function that produces another function. So functional programming is a different way of thinking about code. Most modern programming languages use a mix of different programming types and Java's no different. Although it is very much an object-oriented language, there are ways to do functional programming with it. And two of the features introduced in Java 8, lambdas and streams, allow you to do functional style programming in Java.

Contents