O'Reilly logo

Programming Groovy 2 by Venkat Subramaniam

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required