Seeing the history of Java we can easily say, Java had troubles adapting functional programming. Recently Oracle has made some efforts to bring functional programming pattern to Java. In this blog we are going to discuss about Java lambda and functional programming.
What is functional programming?
Functional programming focused mainly on three points while function creation pure functions, avoid sharing state and immutable variables.
In Java world methods were part of the class and they used to use instance state (object properties) and modify them. But pure function concept says method return should solely be based on method argument and it shouldn’t share state. Immutable variables are never changed in whole function call they create a copy of the value, such as primitive types. Perks of functional programming function can be passed as an argument and returned same as the objects. For example
How Java has added functional programming
To support functional programming Java had to support methods without a class. As you might be aware to create a method in Java we always needed a class but functional programming requires the function to be independent of class.
Here just to add two integers we had to create a class and to access methods you need to create class object and access. But as you can see this function is a pure function, it doesn’t use object state and return the values based on input variables. For this Java 8 introduced function interfaces.
Java 8 lambda and functional programming