Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
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
    
Check JRuby ... –  Heather Aug 16 '11 at 4:52
6  
@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
show 5 more comments

7 Answers

up vote 58 down vote accepted

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:

share|improve this answer
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
    
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
add comment

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.

share|improve this answer
    
+1 For mentioning Akka and the actor model. –  Giorgio May 23 '12 at 20:55
    
"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
show 4 more comments

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.

share|improve this answer
add comment

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. .

share|improve this answer
    
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
add comment

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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