Over a million developers have joined DZone.

Groovy as "Better Java"

· Java Zone

Navigate the Maze of the End-User Experience and pick up this APM Essential guide, brought to you in partnership with CA Technologies

You can use groovy in different ways - for scripting, Grails, quick prototyping, creation of DSLs etc.
But I like Groovy first of all as "better Java". Really, look - almost every Java code is valid Groovy code, i.e. - if you do not remember how to do something in a groovy way, you can write Java code in a Groovy class, but if you remember - use closures, Groovy lists, and many other nice features
The only thing, that does not allow me to use Groovy in a production code instead Java - is the lack of compilation time errors in many cases - calling to non existing methods or variable, etc.

Many Groovy frameworks is using this (see, for example Groovy XML handling ), but if I'm writing regular code, I want to see here compilation errors.
Finally, Groovy 2.0 provides a feature of strict compilation time checking. 

Let's take such class:


It passes a compilation w/o any problem.
But if we will add  @TypeChecked (this annotation may be used for a class or a method), we will receive compilation errors:

This type checking is turned on automatically also if we are turning on static compilation for the class, using annotation @CompileStatic:


So now I can use Groovy for production code, instead of Java!
Full list of compile time messages for @TypeChecked you can see  here.

Thrive in the application economy with an APM model that is strategic. Be E.P.I.C. with CA APM.  Brought to you in partnership with CA Technologies.


Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}