Welcome to edgylogic, drive:activated visitors. This is my new home on the net.


  • JavaFX possibly making Java interesting again

    I'm not a fan of Java. Specifically, the language more than anything else.

    I spent this year learning Java because I had to (I don't think there's a CS course in the world that doesn't use Java somewhere in it), and admittedly, am far from an expert in it, but for the most part it is painful. Not the frustrating 'we do things differently here' painful, but the 'no this isn't possible, here's a long workaround' painful (no properties, no event handling without listener boilerplate every time, APIs without commonly used functions, confusing and subtly changing API, no delegates, no switch on strings, no anonymous functions, complicated UI toolkits, odd access modifiers ... the list goes on).

    Maybe this is because I've been spoilt with C#, and the various new and productive constructs it has to make it significantly different to Java. Or maybe I've been fiddling too much in dynamic languages like JavaScript and Python. These languages have also evolved way faster, finding new ways to allow the developer to be more productive, and for the resultant code to be more elegant. The last significant languages changes for Java was in Java 5, released 5 years ago now. 

    The Java philosophy, and the hardcore followers don't want anything to change, which is fine if it was perfect, but I don't think it is - it certainly isn't something I'd pick to be productive in if it was on language alone. I don't want Java to be C#, Python, Ruby or everything thrown in together - I just want to see solutions to such common pain points in my lifetime (and no, code generation tools are not a solution).

    Which brings me to the point of this post - JavaFX.

    This is the new language the Java guys created, purportedly because it makes creating RIAs a lot easier, but I suspect it is also because the Java guys themselves know there are many improvements that can be made to Java, but they weren't able to for fear of incurring the wrath and alienating large sections of the Java community.

    What makes it cool?

    Well, it incorporates a lot of the productivity enhancing constructs perfected over the years, all while still being very Java-like, statically typed and compiled, and with the full Java library at its disposal.

    Type inference

    This is actually both a blessing and a curse. It allows you to not repeat yourself when you're declaring and instantiating a variable, while keeping clarity. However, if it is used everywhere without thought, it could end up being real messy.

    Declarative construction syntax

    This is JavaFX's answer to MXML (Flex) and XAML (Silverlight/WPF). Instead of using XML, it is a JSON-like (but not entirely JSON) syntax. See here for some examples.

    To be honest, I'm not entirely sold on this. It isn't exactly easier to read, but maybe that's just my eyes being trained on XML over the years. The lack of the new keyword for constructing objects is unsettling too. I guess it is redundant, but it does emphasise the construction part, particularly useful if you have a JavaScript or python mindset and start thinking of dictionaries/anonymous objects.

    Inline string expressions

    So you can do things like:

    var s1 = "java";
    var s2 = "fx";
    var is_fx = true;
    var s3 = "{s1}{if (is_fx) s2}";

    Time literals

    typing 5m will mean 5 minutes to the code automatically - no more thinking in milliseconds!


    This is one of the best new features yet. It is basically list comprehension in Python, or in a restricted manner, LINQ in .NET.

    You can filter arrays of objects by:

    var names = ["Matt", "Peter", "Craig", "Andrew"];  //note the array literal syntax!
    var namesLongerThan4Characters = names[n | n.length() > 4];


    namesLongerThan4Characters = for (n in names where n.length() > 4) n;

    Inserting objects in arrays:

    insert "Alex" before names[0]; //insert Alex at the start of the array

    Slicing arrays:

    var first3Names = names[0..<3]; //note the slice syntax


    Binding in an integral part of the language. To bind something to something else, simply use the bind keyword, e.g.

    var s1 = "hello";
    var s2 = bind s1;
    //s1 and s2 == "hello"

    s1 = "world";
    //s1 and s2 == "world"

    It even works for function arguments, and if a function is a bound function (prefixed with the keyword bound), any instance variables utilised within the function too.

    On replace trigger and new access modifiers

    There still aren't properties in JavaFX as far as I know, but this helps. When you declare a variable, you can declare an anonymous function that gets called when the variable is changed, like so:

    var myName = "Sam" on replace oldName { println("old: {oldName}, new: {myName}"); };

    Combined with this there are new access modifiers, allowing you to designate a public variable as read-only using the public-read modifier, instead of the public one when declaring.


    Yay! Finally, no more writing ugly inner classes boilerplate code for Swing. Anonymous functions can now be created in code, and passed around by variables.

    This example explains the concept, and how it can be used. That wasn't a Swing example though, and I'm not sure what the case is there, given Swing expects Listener classes, not anonymous functions.

    EDIT: Looking at the Stopwatch sample, it doesn't seem like you can use closures/anonymous functions in place of ActionListeners. Back to ugly, boilerplate code again. Wasted opportunity, Sun.


    Check out the official tutorial here. The API reference has some nice stuff too - particularly the effects. It looks a billion times better than Sun's usual API doc style too. And finally, the language reference, back in the typical ugly Sun style.

    So everything's cool right?

    I'd love to say yes, but sadly it isn't. Sun has a history of shooting itself in the foot with Java, and this is no exception.

    (Below is from what I gather on the net - I have not tested the code to see if it actually is missing, so I could be wrong.)

    JavaFX was announced prematurely - bits were released too early. In between releases, there have been very significant changes, enough to break most samples and guides out there. Then apparently, Sun had a change of heart, and decided they didn't like it being an intrepreted language anymore, and it should be compiled instead. Along with this change, brought a huge range of language feature cuts, rendering many, many more samples and guides out there useless (or soon to be if/when the interpreted version is stripped out).

    Just look at this post from Sun themselves, and compare it to the official JavaFX 1.0 tutorial to see what's been cut.There were some really neat features there too, like the select keyword, triggers, and the do loops. Documentation was and still is on the thin side - those blog posts out there really helped, but now many are basically useless because of the changes, yet the average developer googling for help won't know, because to them, it is all still JavaFX Script and will become an exercise in frustration.

    And from what I see on the net, it seems like this change was unexpected, apart from the beta tag, which has lost its meaning anyway.

    On top of this, Sun is stretching the cross-platform marketing point by not releasing a Linux version. Yes - JavaFX only works on Windows and Mac at the moment, disappointing masses of developers out there who were betting on Linux availability as a major advantage. Depending on how Sun works, Moonlight 2.0 and Eclipse4SL might even be out before JavaFX for Linux is. Of course, Flex Builder 3, Flash and AIR are all available on Linux, now.

    Then there's the persistent problem of Java's performance and stability woes. Java has never been very smooth for me, and it is particularly bad inside browsers. It has a uncanning ability to crash your browser just when you need it least, or just freeze it for ages, leaving you to ponder - do I kill it and lose all my tabs and sessions, or wait, and pray that it comes back to life.

    Even with Java 6 Update 11, which promised performance and stability fixes, the sample JavaFX applets consistently killed the tab in Google Chrome, and only 2 of the 4 applets I tested loaded in Firefox, both after rather long waiting periods when compared to something of similar functionality in Flash/SL. And the security restriction dialogs - maybe that's where Microsoft drew their inspiration for UAC from. The drag to break out of the browser trick is cool though.

    One final thing - they've decided to allow JavaFX to play video in a selected set of cross-platform codecs, and also platform-dependent codecs. Yes - you can use JavaFX to play Windows Media Video files, but only on a Windows computer. I hope they make this clear to developers, but seriously, why even offer this option - it only allows developers to create platform-dependence, the exact problem Java has been claiming to help solve all these years.

    So who's happy?

    I'm struggling here. They've thrown up numerous walls to stop newcomers from trying it out, particularly when compared to their stiff competition, Flex and SL. Many old timers dislike change, and probably won't like the new syntax and way of doing things. Some might. And a few open source fanatics might jump on board, just because it is open source (even though it isn't completely yet, and as I mentioned before, there's no Linux version).

    Yes it has cool tricks, can do things neither Flash/Flex/AIR or Silverlight can do, has the large Java community it can leverage, and has a wide range of components and libraries from Java to support it already, but that all means nothing if there's insufficient/confusing documentation, and the user experience sucks.

    Here's hoping that the community helps you out Sun, and the next iteration of JavaFX is massively improved upon, because JavaFX just doesn't seem like the magic bullet you needed to stay ahead of the game.

    Then again, maybe I haven't drunk enough of the Java kool-aid Smile

    UPDATE: Changed the starting paragraph - sounded way too harsh and set the wrong tone.

    UPDATE (07/01/2009): Looks like Java 7's been pushed back to 2010, and the list of new features is... rather short and boring. At least I'm not the only one disappointed with Java's development. http://www.infoq.com/news/2009/01/java7-updated




Alot of your points are quite valid. Java is rather bloated and is desperate in need of a refresh. And even Google themselves are stepping in. (More on that later)

Re: Listeners

This is tricky. I'm familiar with creating my own Java Listeners and I can understand why they did it that way. Are you referring to implementing event listeners in Swing? Or are you referring to creating your own listener interface? I suspect that if you tried to write your own interface you might see the benefits, despite the "boilerplate" code you have to write each time.

And furthermore, Sun probably thinks anonymous functions are simply an extra feature too many to bother with. Like you said, there hasn't been much change to the language since Java 5.

Re: applets

Ew. I stay far away from Applets. If I need anything I use Flash/Flex.


I think Java as a language is fine. C# borrows many of its "features" from Java, which says alot. The Java Platform, however, is horribly broken. I shall therefore try to inspire you to stick with Java by pointing you to

Dalvik, which is Google's nonstandard Java standard which their Android applications are built on. Although Google seems to be a bully in this situation, I think it would benefit Java as a whole.


Daryl - "I code Swing applications without a UI toolkit"

Posted by Daryl Teo, 5th January 2009 12:46 AM


I don't like the fact that I need to create a new class to handle an event, when all I need is a function. It also bloats out my project with class files everywhere (or with inner classes, excessive code). The overhead in declaring events also means the listener pattern isn't used as much as it could be.

Maybe anonymous functions aren't the answer, but maybe function pointers? I'm thinking of how it is done in AS3 (addEventListener(event, function)), JS (addEventListener(event, function)), C# (object.event += delegate(function)) and VB.NET (Sub objectEvent(...) Handles object.event) here - the code just seems cleaner, and flows better.

As for C#'s inspiration, it was indeed using a sound base back then. But Java has barely changed, let alone evolved. Surely there are better ways of doing things now that people didn't think of 5 years ago?

Dalvik is interesting, but it still has a long way to go to gain recognition and distribution on platforms other than Android phones. It could be the spark to start Java moving again though.

Cheers for the comments!

Posted by [edgylogic] Sam, 5th January 2009 11:38 AM

All fair points apart from the bit about XML.  The widespread use of XML alongside Java is precisely because Java is so bad for programming in a declarative style.  JavaFX fixes that.  Why would you use an irritatingly verbose document format to write programs instead of a purpose-built declarative programming language?  His trivial example looks fine but doesn't contain any code to perform actions, only declarative layout code.  How would he propose to encode that in XML?  XML makes a very bad programming language as anyone who's worked on a moderately compliated Ant build will testify.

Posted by battery, 21st October 2009 8:50 PM

New comments have been disabled for this post.

If you have something to ask about this post, drop me a message.