Java Lambdas

This shall be a brief overview over Java Lambdas. I will try to make a comparison to anonymous functions concepts how they are being used in other languages. As a comparison language, I choose Scala, since it is statically typed (just like Java is). However, Scala provides type inference, which makes it feel more like a dynamically typed language at times although it is not. For this little comparison though, I won’t make use of type inference, because I want to focus on the conceptual differences.

At first sight there is not much difference here (apart from the fact that Java is somewhat more verbose, just like you would expect) compared to the Scala code.

At a closer look though, two differences attract attention:

In the Java code there is an interface involved (java.util.function.BiFunction), while Scala features a native type for anonymous functions.

In Java, in order to invoke the anonymous function, you actually call a method of the interface (apply), while in Scala you can call it just like any other function.

Interfaces instead of native function types

Well yeah, in order to assign a lambda to a variable, that variable of course needs a type, as we are dealing with statically typed languages here. The fact that Java is using interfaces here instead of native types makes handling an anonymous function somewhat more clumsy than it would otherwise have to be. You have to import that Interface while in Scala you can just type away the applicable type declaration.

Well in the package java.util.function you will find a whole bunch of predefined Interfaces that might be sufficient for you.

If you encounter a case in which you don’t find an interface that applies to your use case, you will have to define your own:

Ah well… not really something that you’d call elegant, huh? In fact that means you either write lambdas that match one of the interfaces in java.util.function or you define your own interface. That is not exactly what someone who is used to functional programming would expect. However, it saved the Java developers a whole bunch of problems, since they certainly didn’t have to change the type system much (if at all) to introduce this kind of lambdas.

Passing around Lambdas

Now what does that all mean when we want to pass around an anonymous function. A common use case eventually, because that is what makes functional programming powerful, e.g. separating business logic from algorithms, having functions apply other functions to data in a certain manner, without having to know that that other function is doing.

Java Lambdas however turns out to not allow modifying state of free variables (i.e. the variables the anonymous function closes over, sum in this case). This means the following, equivalent Java code does not compile:

The compiler will complain about that sum is not declared final, due to trying to access it from within the lambda. Declaring it final though means that you won’t be able to assign a new value to it during a call to the lambda, rendering implementation of our accumulator in this way impossible.

From within Java lambdas you can read free variables though.

Maneuvering around the Java Closure “final” restriction

You can of course avoid this restriction by not assigning a new value to the free variable directly, but arranging it to be a container for the value you want to represent and then call methods on that object to mutate your state within that object. We could for example make sum an AtomicInteger and then call get() and set() on it instead of assigning it a new value directly (thanks to Dave for pointing this out):

However, this feels like cheating a bit and is not as concise as it could be.

Conclusion

Lambdas in Java are definitely an improvement to the language. However, they still fall short on several expectations that you’d usually have when talking about anonymous functions, lambdas and functional programming:

You’d expect them to have a native type (instead of taking an indirect route via interfaces)

You’d expect that you can call them using the normal call syntax of your language (i.e. fn(1, 2) instead of fn.apply(1, 2) )

You’d expect to get full featured lexical closures – including mutation of free variables, as this is an important aspect for why closures are powerful.

So Java Lambdas feel a bit restrained, especially when you’ve experience with anonymous functions in other languages that really treat lambdas as very native features of their own. The Scala code here is even more verbose than it would usually be, as I didn’t make use of Scala’s type inference. If you start throwing dynamically typed languages into the mix, the discrepancy between Lambdas as they are now implemented in Java and these languages will even grow.