Support of Java 5 Language Features

Java 5 language features like enums and annotations work in Groovy also. This means we can mix Java and Groovy quite fluently. To refresh, the Java 5 language features are as follows:

  • Autoboxing

  • for-each

  • enum

  • Varargs

  • Annotation

  • Static import

  • Generics

Let’s discuss the extent of the Groovy support for these features.

Autoboxing

Groovy, because of its dynamic typing, supports autoboxing from the get-go. In fact, Groovy automatically treats primitives as objects where necessary. For instance, execute the following code:

GroovyForJavaEyes/NotInt.groovy
 
int​ val = 5
 
 
println val.getClass().name

The type is reported as follows:

 
java.lang.Integer

In this code, we created an instance of java.lang.Integer ...

Get Programming Groovy 2 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.