Over a million developers have joined DZone.
Platinum Partner

Is it Time for Functional Programming in Java?

· Java Zone

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.

Fuctional programming has existed for decades, but only in the last few years has it gained the attention it deserves, for a lot of very good reasons like:

  • it reduces code duplicarion
  • it improves readability
  • it allows better reuse
  • it facilitates multi-threading programming
  • it eliminates ugly programming by side-effects

Unfortunately Java doesn't natively allow you to take advantage of functional programming. It seemed for a couple of years that Java 7 was going to fill this lack by implementing the BGGA specification. But in the end they decided to keep BGGA out of Java 7, disappointing for the functional programming fan. This is one of the reasons why many Java programmers are migrating (or at least evaluating to migrate) toward more modern JVM compatible languages like Groovy and Scala. These languages allow developers to leverage the features of both object oriented and functional programming without losing the reliability and portability provided by a rock-solid and mature runtime environment like the JVM one.

Anyway, if you don't want to learn a completely new language (even if there could be some good reasons to do that as outlinedĀ  here) or more likely you are not allowed to use something different to Java, you could still introduce some functional programming techniques in your code. Indeed there are some tools and libraries available that allow you to do that in Java.

Probably the best known of these library is functionalJava which offers the possibility to write code following the BGGA specification already in Java 6. However this solution has, in my opinion, some important drawbacks like the needs to preprocess your code in order to translate the BGGA statements and the complete lack of support for the BGGA syntax inĀ  IDEs.

Another tool that could serve to this purpose is functionalj. It has the advantage of not requiring any code translation, but it also looks far less powerful than functionalJava and doesn't seem to offer a satisfying generics support.

In the end, with its 2.0 release, lambdaj has provided a third option for functional programming in Java by allowing developers to define and use first-class functions, in a more readable DSL style. It seems to combine the pros of both the former libraries, but at the same time it presents some important limitations due to the heavy use of proxy made in its implementation.

To summarize if, like me, you feel the absence of functional programming in Java, these tools, despite not being perfect, can be a valuable replacement while waiting to finally see those features that will be available natively in plain Java.

The Java Zone is brought to you in partnership with ZeroTurnaround. Discover how you can skip the build and redeploy process by using JRebel by ZeroTurnaround.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}