Java Forum Nord 2015 - Functional Programming in Java 8 by Nicole Rauch

This article is part of a series about Java Forum Nord 2015, a conference that took place in Hannover, Germany. Links to articles about other talks I visited there can be found below.

I managed to avoid the topic of functional programming for a long time, which was not good. When functional concepts have been implemented in Java 8 I thought “He cool, now I must not even leave my favorite programming language to learn functional programming”. I enjoy using these features and want to learn more about functional programming. This gave me more than enough motivation to visit Nicole’s talk.

Nicole began her talk by asking the audience how they would implement an application that sums the squared numbers from 1 to 10 in “good old Java”. The solution is the following for-loop:

int sum = 0;
for(int i = 1; i <= 10; i++) {
    sum = sum + i*i;

Then, she wrote the same code with Java 8 (using streams):

IntUnaryOperator square = x ->; x*x;
IntBinaryOperator add = (x,y) -> x+y;
IntStream.rangeClosed(1, 10).map(square).reduce(0, add);   // 385

That looks great and way more readable. Then she showed the same in Haskell:

foldl (+) 0 (map (\x ->; x*x) [1..10])   -- 385

There are so few characters I think it cannot be any shorter. Right after that and a few other code examples I thought “I have to learn Haskell”. Because I don’t want to cite all of her slides here, I just take one of her examples. You can (and should!) have a look at the rest of her slides.

Nicole explained the main concepts of functional programming. A functional language has to include immutability and pure functions and treat functions as first class citizens.

So, what is immutability? An immutable object is an object that doesn’t change. In Java, this can be done by never writing set()-methods and also never change the attributes of an object. Instead, objects are created by giving them all attributes in the constructor. Changes are realized by creating whole new objects instead of changing existing ones. This makes these objects thread-safe, because they have a consistent state at any time. Also, they are way easier to construct and test.

A pure function simply is a method without side effects. Its output is only dependent from its input parameters, which is called “referential transparency”. If this is true, then this method will give the same result when being called multiple times with the same parameters. Everyone who has debugged a nice, weird legacy codebase knows: These methods are way easier to understand than methods with side effects. Also, they are optimizable by the compiler because the order of execution is arbitrary and results can be cached.

Treating functions as first class citizens means that a function can be referenced and passed around wherever needed.

This makes it possible to use functions as parameters for other functions. These are called higher order functions, because they take other functions as parameters.

Using these concepts results in a number of advantages. There is no complicated modelling of status changes while debugging. Several optimizations can be done automatically. The code is more robust. Effects of code are isolated in one scope and need not be tracked throughout the codebase.

Other content of Java Forum Nord 2015

These are the talks I visited:


At Java Forum Nord 2015, Nicole Rauch gave an overview of the most important aspects of functional programming both in Java and Haskell.