No need to wait - Java 8, it's great

Matti Tahvonen
Matti Tahvonen
On Jan 20, 2014 8:09:00 AM

NetBeans 8 beta suggesting to use a simpler lambda expression as listener
NetBeans IDE 8 automatically suggests to use Java 8 lambda expressions

Java 8 is to be released soon. The current schedule predicts release in the middle of March, but you should notice that the recent postponements have been due to the various webstart related security enhancements. For web developers, it has practically been ready for the whole autumn already.

I have been hacking with Java 8 lately to share some insights with all you Vaadin enthusiasts. In this blog entry I’ll not try to cover everything that is new in Java 8, but the most essential enhancements that I think are relevant for all Vaadin users.

Java 8 means largish enhancements to both JVM and language. The good thing is that changes are mostly backwards compatible, especially if you are not working with low level libraries. For example with core Vaadin, I haven’t found any issues so far.

Still, although the core Java 8 is practically production ready, I wouldn’t suggest to try Java 8 in your enterprise projects with tight deadlines today. During my exercise, I found Java 8 as a JRE already to be superior to its older sisters*, but dealing with new Java 8 bytecode still seems to be the culprit for some libraries and servers. E.g. I fought with my favorite test server for quite a while, just to notice its annotation processing library doesn’t support Java 8 byte code without hacks yet *.

Although some library and server developers still have things to do with their Java 8 support, I sincerely suggest to start learning the goodies of Java 8. Also, in less critical systems, I wouldn’t be afraid to start using it in production. A combination of tools I found working well:

  • Java 8
  • NetBeans IDE 8.0 Beta (7.4 also supports Java8, but 8 has more code converters and Java hints)
  • Latest Vaadin plugin
  • GlassFish Server 4 (bundled with NetBeans IDE)

Lambdas will clean your listener code

Java has never tried to minimize your loc count. With good typing and nice code completions in IDEs, you don’t really need to minimize the amount of code you write. Maintainability and readability is commonly way more important than the amount of code. Still even, hardest Java fanatics have to confess that lots of keywords and symbols don’t necessary improve your code quality.

Lambda support is one of the most visible enhancements in Java 8. In Vaadin developers’ daily work, lambdas mean a cleaner syntax for inline event listeners: any single method interface can now be expressed with a shorter lambda expression. From a library developers POI, this is a great change. Most commonly there are no changes needed to the libraries. Let me repeat myself: most core Vaadin API’s and add-ons can take advantage of lambdas today.

As where previously an inline click listener for a Button looked like this…

button.addClickListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        // Your code here

… it can now be expressed like this…

    // Your code here

… or as a one-liner like this:


button.addClickListener(e->System.out.println("Yes, it works"));


Method name listeners, welcome back

If you have been following the Vaadin development for as long as I have been involved with it, you know that we once had an API that allowed the usage of method names together with the target object as “listeners”. It is arguable a handy way to hook your logic to views, but on the other hand it is a very risky place to introduce bugs. Also, you’ll lose all the cool refactoring support that IDE’s provide. So, we finally deprecated the feature in 2013 in Vaadin 7. IMO we should have deprecated them already in 2008.

A year later, in 2014, method name listeners are making their comeback. This time without compromises nor intervention from our R&D team. Now with Lambda enhancements in Java 8, you can reference a method from an object, with a compatible signature, and use it as a listener. E.g. hooking your view to a controller might look like this in the future:




More lambda goodies

One thing to note is that lambda expressions are not a shorthand for inner classes. They are a bit larger enhancement to the language and JVM than they might seem at first sight. When starting with lambdas, variable scoping is something where you should be awake. In most cases things will just work, also in cases where it previously might have not. If you are an experienced Javascript hacker, you could consider lambdas as closures.

A short example here might tell more than a longish technical explanation:

LocalDateTimeField field = new LocalDateTimeField();
    /* Look Ma, no "final" required for the field variable! */"Value now:" + field.getValue());


If you have worked with Java in any non-trivial project, you must know something about the history of handling dates and times in Java. I know that Date and time calculations are not easy, but still, Java has a bad history in this area. So bad that in many Java projects such a generic thing, handling dates, needs a third party helper library. Yes, that is JodaTime.

Now, in Java 8, it finally seems to contain a date handling library whose quality is in par with the rest of the JDK libraries. At least I haven’t heard too much bad about the new API’s yet.

To get started with the new date and time types, I started a new add-on project called java8date. Although I can’t really praise the core Vaadin DateField either, I based my initial solution on it and on some custom field magic. The first component in my add-on is a field for LocalDateTime, but I hope to find time to add support for other types as well.

Use the add-ons github page for feature requests, patches for other types and bugfixes. I’ll be more than happy to pull in your contributions!

Other changes in Java8

This was just a scratch on the surface about enhancements in Java 8. There are for example default methods for interfaces, big improvements to collections and their parallel processing. All the new changes don’t directly affect working with Vaadin API’s, but they are of course essential improvements that make building Vaadin applications (with Java) smoother. If you come up with other goodies Java 8 provides for Vaadin usage, please let us all know.

Big thanks to Oracle and other Java 8 developers, your contributions have made also Vaadin a much better tool!

Matti Tahvonen
Matti Tahvonen
Matti Tahvonen has a long history in Vaadin R&D: developing the core framework from the dark ages of pure JS client side to the GWT era and creating number of official and unofficial Vaadin add-ons. His current responsibility is to keep you up to date with latest and greatest Vaadin related technologies. You can follow him on Twitter – @MattiTahvonen
Other posts by Matti Tahvonen