Java, Groovy & JavaFx: side to side 2 - No Fluff Just Stuff

Java, Groovy & JavaFx: side to side 2

Posted by: Andres Almiray on August 19, 2008

Second part of the Java,Groovy,JavaFx series. The following table details the language features introduced in JSE 5 and how they may or may not be supported by Groovy and JavaFx.

Feature Java Groovy JavaFx
Varargs class Person {
   public void greet(Object... args) {
     // access the "args" array here
   }
}

note: works on JDK5+
class Person {
   public void greet(Object... args) {
     // access the "args" array here
   }
}

note: works on JDK5+

class Person {
   public void greet(Object[] args) {
     // access the "args" array here
   }
}

note: any array type will do only if it is the last parameter defined. Works on JDK4+
Not supported
Annotations @NotNull String field;
@NotNull String field;


note: annotations work in the same way as in Java, with the caveat that they can not be defined in Groovy yet, requires JDJ5+

update: Groovy 1.6 allows defining annotations in Groovy :-D
Not Supported
Generics class MyComparable implements Comparable<MyClass> { }

Map<String,Integer> map = new HashMap<String,Integer>();
class MyComparable implements Comparable<MyClass> { }

Map<String,Integer> map = new HashMap<String,Integer>();


note: generics work in the same way as in Java, requires JDK5+
Not Supported
Static imports import static java.awt.Color.RED;
import static java.awt.Color.RED;


note: static imports work in the same way as in Java, requires JDK5+
Not Supported
Typesafe Enums enum Lang {
  JAVA, GROOVY, JAVAFX;
}
enum Lang {
  JAVA, GROOVY, JAVAFX;
}

note: enums work in the same way as in Java, plus they receive some extra behavior from the GDK
Enum definition is not supported, but you can access them as any regular Java class, requires JDK5+
Enhanced for-loop for( Type variableName : Iterable )
  // statements }
for( Type variableName : Iterable )
  // statements
}

Groovy has its own enhanced for loop syntax too

for( Type variableName in Iterable )
  // statements
}

note: a type must be defined in the first version (requires JDK5+), type is optional in the second version
JavaFx has its own enhanced for loop syntax, can chain sequences

for (album in albums, track in album.tracks) {
   if (album.title == track)
     // do x
   else
     // do y
}

it also accepts filters (with a where clause)
Autoboxing/Unboxing int i = Integer.valueOf(10);
Integer j = 42;
Everything in Groovy is an object. Still it will perform autoboxing/unboxing when calling Java code that requires either a primitive or a wrapper value. JavaFx does not have primitive types per se, rather they are called basic types: String, Boolean, Number, Integer, Duration (Basic Types table)


Clearly Groovy has a better integration with Java the language than JavaFx does, but I must remind you that is by design of both languages. Developers that find themselves comfortable enough with Java5 features should feel right at home should they decide to pick Groovy, those who choose JavaFx will find themselves relearning new ways to do what they are used to, which is not really a bad thing per se, just beware of the context switching when programming in 2 or 3 languages that sport considerable differences feature wise and syntax wise.
Andres Almiray

About Andres Almiray

Andres is a Java/Groovy developer and a Java Champion with more than 20 years of experience in software design and development. He has been involved in web and desktop application development since the early days of Java. Andres is a true believer in open source and has participated on popular projects like Groovy, Griffon, and DbUnit, as well as starting his own projects (Json-lib, EZMorph, GraphicsBuilder, JideBuilder). Founding member of the Griffon framework and Hackergarten community event. https://ch.linkedin.com/in/aalmiray

Why Attend the NFJS Tour?

  • » Cutting-Edge Technologies
  • » Agile Practices
  • » Peer Exchange

Current Topics:

  • Languages on the JVM: Scala, Groovy, Clojure
  • Enterprise Java
  • Core Java, Java 8
  • Agility
  • Testing: Geb, Spock, Easyb
  • REST
  • NoSQL: MongoDB, Cassandra
  • Hadoop
  • Spring 4
  • Cloud
  • Automation Tools: Gradle, Git, Jenkins, Sonar
  • HTML5, CSS3, AngularJS, jQuery, Usability
  • Mobile Apps - iPhone and Android
  • More...
Learn More »