Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Groovy as "Better Java"

DZone's Guide to

Groovy as "Better Java"

· Java Zone ·
Free Resource

The CMS developers love. Open Source, API-first and Enterprise-grade. Try BloomReach CMS for free.

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.

BloomReach CMS: the API-first CMS of the future. Open-source & enterprise-grade. - As a Java developer, you will feel at home using Maven builds and your favorite IDE (e.g. Eclipse or IntelliJ) and continuous integration server (e.g. Jenkins). Manage your Java objects using Spring Framework, write your templates in JSP or Freemarker. Try for free.

Topics:

Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}