I was browsing through the Amazon.com Bookstore and I came across the book "Functional Programming for Java Developers".

I know some very basic Functional Programming and have been programming in Java for 3 years.

I would like to know is Functional Programming even possible in Java?

  • 2
    The style is possible but its verbosity could be too much to bear given Java's syntax: functionaljava.org – Yuriy Zubarev Aug 16 '11 at 2:06
  • 7
    @nCdy Why to check JRuby? please provide more explanation. – Chiron Aug 16 '11 at 5:06
  • 1
    check groovy :-) – Ant's Aug 16 '11 at 5:18
  • 6
    I think this is the kind of question that raises the distinction between Java-the-language and Java-the-platform. – Thomas Owens Aug 17 '11 at 14:52
  • 2
    @ThorbjørnRavnAndersen: What makes you think that "functional programing" is defined by "lazy evaluation"? It seems an odd example to pick... – John Bartholomew Mar 8 '12 at 9:24

It depends what you mean by "functional programming" and by "possible".

You can obviously implement things following a functional paradigm. However the Java language doesn't provide the syntactic sugar for it, so some things will be tedious at best, and some other ones will be extremely arcane.

Similarly, you can very well write object-oriented code in a language recognized as being non-OO, like C.

Java Libraries

There are libraries that can help you do this, by already doing the legwork for you and hiding the arcane things:

These will allow you to write Java code with a more functional approach and possibly more familiar syntax and semantic, as you'd expect from an FP-competent language. Within reason, that is.

JVM Languages

And obviously, you can implement a functional language on top of Java. So that you can then use that one as your FP language. Which is a bit of a higher-level of abstraction than what you asked for, but relatively within context (though I'm cheating a bit here, granted).

For instance, check out:

More-or-Less Functional JVM Languages

While they may not be exactly what you want, there are a number of other languages that have been ported to the Java Platform and that might free you from Java's relatively not so fun-oriented (yes, pun intended) nature and already give you more flexibility. Notable contenders like JRuby, Jython and Rhino (respectively for Ruby, Python and JavaScript / ECMAScript) also offer interesting potential for functional programming, though they arguably aren't really functional programming languages by nature. JetBrains' Kotlin, while clearly acknowledging it isn't a functional language, does support some functional constructs and is also worth a look.

Further Reading

You may also want to read or watch these articles or videos:

  • 2
    Clojure source code is now hosted on Github github.com/clojure/clojure – Chiron Aug 16 '11 at 2:34
  • @The Legend of 1982: I was faster than your comment, I already changed the link to the official clojure.org site :) But thanks for catching it so quickly! Good to see people react fast. – haylem Aug 16 '11 at 2:36
  • There's also ABCL (common-lisp.net/project/armedbear), but I have no idea where it falls on the mature/no mature scale and it's a Common Lisp implementation. – Vatine Mar 8 '12 at 11:45
  • @Vatine: interesting, had absolutely never heard of it. Will have a quick look and add it. – haylem Mar 8 '12 at 12:03
  • 1
    I love Alan Perlis's term "Turing Tarpit" for trying to do something that a languge can do (because it is turing complete) but only with such pain and complexity that it should not be attempted. – itsbruce Oct 6 '12 at 11:22

I'm reading the book you mentioned. It is really good BTW.

Yes it is possible to be functional in Java. I don't know to which degree you can achieve it but you can implement many functional programming idioms.

One of the most important things is trying to code with "Don't mutate states" mentality.

For example you use the final keyword to achieve immutability. If you are going to use a data structure then you should code in immutable data structures. Google Guava library is already doing this.

Also for concurrent programming, you can rely on Akka framework (the actor model).

It is worth mentioning that the JVM bytecode doesn't support (at least yet) Tail-call optimization, a very important feature for functional programming languages.

  • "It is worth mentioning that the JVM bytecode doesn't support (at least yet) Tail-call optimization, a very important feature for functional programming languages.": What do you mean? Scala does have TCO. – Giorgio Oct 6 '12 at 12:33
  • @Giorgio: Scala does TCO at compile time. – scrwtp Jan 25 '13 at 0:46
  • @scrwtp: Is there a performance penalty because of that? Or what are the disadvantages of compile-time TCO? – Giorgio Jan 25 '13 at 6:36
  • @Giorgio: What I meant is that java bytecode does not natively support it, just like Chiron originally stated. Functional languages work around it, compiling tail-recursive calls into loops, but it's a feature of the language->bytecode compiler, not the JVM. – scrwtp Jan 25 '13 at 9:22
  • @scrwtp: Chiron's answer states "It is worth mentioning that the JVM bytecode doesn't support (at least yet) Tail-call optimization, a very important feature for functional programming languages." making it sound like an implementation of a functional language on the JVM is penalized but (and we seem to agree on this) this is not true since tail call optimization can be simulated in the compiled code. So I agree with Chiron's statement but I find it a bit misleading. – Giorgio Jan 25 '13 at 9:51

Yes, it is definitely possible, in the same way that it is possible in any turing-complete language / execution environment combination. You can even get it to perform quite well if you know what you are doing.

Not sure how sensible it is though. In particular, be aware that it's not particularly idiomatic (i.e. it will look very strange, you will have to do some unconventional things and confuse people who are used to regular Java)

You'll end up with some odd looking code, e.g. to define a new function:

Function twoStrings=new Function() {
  public Object apply(Object param1) {
    // do something to param1 and return a result

To do functional programming you typically need:

  • First class functions - easy to create in Java by defining an abstract class or interface that represents your "Function" and has an "apply" method which applies the function to one or more parameters.
  • Closures - create an instance of your function object above with the closed-over values stored in final fields. You can use an anonymous inner class for this.
  • A library of standard higher order functions - this is trickier, however you can still write your own to bootstrap a simple functional language in a few hours. If you want something fancier, you can check out other functional libraries people have built in Java.

So it's possible as an exercise and even an interesting hobby project. But you really want to do serious functional programming while maintaining the advantages of the JVM / accessing Java libraries then Clojure is by far your best option in my opinion.

p.s. since the core of Clojure is actually written in Java, it's actually a very interesting case example of how to do functional programming in Java while hiding the messy details behind a nice new modern language syntax. Clojure source code is on GitHub for those interested.

  • Interestingly, I've noticed that the latest version of IntelliJ IDEA will fold these anonymous onesies into a more compact form for display in the editor. Does a pretyt decent jobs of hiding the redundant cruft. Of course all this will be irrelevant come Java 8. – Ben Hardy May 27 '12 at 22:53

It is possible to be somewhat functional in Java. Doing so is seriously painful. Instead of

myList.each { doSomething(it); }

You have something like:

myList.each(new Function() { public void do(Object arg) { something(); }})

And if you want true functional programming, with closures and functions as first class objects, pick another language. Scala, Clojure, Groovy all run on the JVM and can interact with legacy Java classes. .

  • Functional programming aren't about using blocks instead of anonymous classes. Java 8 is going to has blocks, so your posted code will looks more elegant but it is not going to be functional programming. – Chiron Aug 16 '11 at 4:08
  • @Legend: I understand that, but evidently didn't explain it well. – kevin cline Aug 16 '11 at 4:42
  • this is oversight/undersight. with any language, you have to actually define the function SOMEWHERE, you can't skip that part. So Java is almost as concise, all you have to do is make the Function object unanonymous. You can do this: Function f = new Function() { public void do(){}}; ....then...call that function....myMethodToCallAFunction(Function f){f.do()}...that's it, bros and brolinis. deal with it. – Alexander Mills Oct 7 '14 at 6:00

The answer is a resounding "yes, of course", yet in my opinion one of the most important features in many functional languages is the excellent type system. You'll never be able to manage this in Java yourself.

If you want to write functional programs and yet stay with the JVM I can recommend among the usual suspects Scala and Clojure to look at Frege. Frege has a syntax and type system that is very close to Haskell, but programs are translated directly to java code and can interact with other java code.


Well, all sorts of things are possible. It's possible to do object-oriented programming in C; it's just not a very good idea.

Java wasn't designed for FP, so if you're trying to do everything in a purely-FP style, you'll have problems. You'll be fighting the language, instead of working with it. And not just the language - there's also all the wonderful free Java libraries. So don't go for pure FP; take some of the ideas behind FP and integrate them into your Java code, but understand that you can't do that to all the ideas.


You're encouraged by the OpenJDK team to download their latest OpenJDK 8 binaries and play around with the new lambda expressions and the new functional idioms introduced in the Collections API (among others). You can program in a clear functional style. See "Functional programming in Java?" for a JDK8 Collections comparison with pre Java8 libs like Guava, FunctionalJava and LambdaJ.


It may seems possible but it will not be a pure functional programming. It may results to imperative programming.

There is no asking why he mean by possible functional programming as mentioned by haylem. Here it is:

It depends what you mean by "functional programming" and by "possible".

Functional programming cannot have different definitions or meaning though it may have many explanations.
Like OOP, can we ask "what do you mean by OOP?".
Definitely there will be lot of explanations but it will just pertain to one objective, the objective of OOP.
The same applies to functional programming.

When we say functional meaning the programs consist of functions.
The role of the functions is to return an evaluated argument/parameter (argument is variable is the expression came when calling the function while parameter is variable that is a part of the function declaration).

Also functions will always return the same result when same arguments is passed. In that way it is easier to avoid bugs or debug future bugs. By functional programming we can avoid side effects like modifying global variable.

example in JavaScript:

function increment(lis){
    return lis.map(
        function (x){
            return x+2;

var myList = [4, 7, 2, 3];

The function increment adds 1 value to each of the element inside object and return the result. The value of myList did not change but when we call the functions we saw the added value to elements of that object.

As my response to Is Functional Programming possible in Java?, I believe that it is not possible to have true functional programming in java. Because java is really designed to be OOP in which it extends imperative programming and improved it for maintainability. When the state of an object, variable etc, has changed then, that is already an imperative programming.

Not the answer you're looking for? Browse other questions tagged or ask your own question.