Groovy and Grails: Concepts, Examples and Methods
19 July, 2008 at 6:06 pm Leave a comment
Groovy is a language that has a syntax that’s similar to, yet simpler than, Java. It’s often referred to as a scripting/agile/dynamic language, but I would prefer to stay away from these adjectives as I feel they only end up confusing things. If Java is a wise middle-aged man, Groovy is his teenage son. Groovy has many of the old man’s characteristics but is a lot wilder and a lot more fun. Both of them also work together very well.
Groovy has a lot fewer rules than Java. For example, in Java to get the standard “Hello World” output, you need to write a class, a main method with proper arguments, and more. But in Groovy, if you don’t wish to write all the boilerplate code, you can get rid of the class definition and the main method and just write the one line of code that actually prints “Hello World.”
So the contents of a file Hello.groovy that prints Hello World would be as follows:
println "Hello World"
The Java platform is concerned only with getting bytecodes to execute. As such, the platform does not force you to use the Java language. As long as you provide bytecodes, things will work. Groovy compiles to bytecodes, and it makes no difference to the Java platform if the bytecodes were generated from code written in Java or Groovy.
Here is an example of Groovy that shows Groovy’s built-in support for lists, maps, and range and also demonstrates the simplicity of Groovy and its capacity to leverage the power of Java:
// Print Date
def mydate = new java.util.Date()
println mydate
//Iterate through a map
def numbersMAP = ['1':'ONE', '2':'TWO']
for (entry in numbersMAP) {
println "${entry.key} = ${entry.value}"
}
//Introducing the range
def range = 'a'..'d'
//Lists
def numberlist = [1, 2, 3, 4, 5, 6, 7, 8]
println numberlist;
println "Maximum value: ${numberlist.max()}"
Note that the above code directly uses java.util.Date and that the built-in support for collections cuts down on code required to work with lists, maps, and ranges. There are many other interesting Groovy features like closures and simplified XML processing. You can find a detailed listing at groovy.codehaus.org.
Grails is an open source web application framework which leverages the Groovy programming language (which is in turn based on the Java platform). Grails is intended to be a high-productivity framework by following the “coding by convention” paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer.
Grails was previously known as ‘Groovy on Rails’ (the name was dropped in response to a request by David Heinemeier Hansson, founder of the Ruby on Rails framework.[1]). Work began in July 2005, with the 0.1 release on March 29, 2006 and the 1.0 release announced on February 18, 2008.
Differences from Java
Groovy tries to be as natural as possible for Java developers. We’ve tried to follow the principle of least surprise when designing Groovy, particularly for developers learning Groovy who’ve come from a Java background.
Here we list all the major differences between Java and Groovy.
Default imports
All these packages and classes are imported by default, i.e. you do not have to use an explicit import statement to use them:
- java.io.*
- java.lang.*
- java.math.BigDecimal
- java.math.BigInteger
- java.net.*
- java.util.*
- groovy.lang.*
- groovy.util.*
Common gotchas
Here we list the common things you might trip over if you’re a Java developer starting to use Groovy.
- == means equals on all types. In Java there’s a wierd part of the syntax where == means equality for primitive types and == means identity for objects. Since we’re using autoboxing this would be very confusing for Java developers (since x == 5 would be mostly false if x was 5
. So for simplicity == means equals() in Groovy. If you really need the identity, you can use the method “is” like foo.is(bar). This does not work on null, but you can still use == here: foo==null. - in is a keyword. So don’t use it as a variable name.
- When declaring array you can’t write
int[] a = {1,2,3};you need to write
int[] a = [1,2,3] - If you were used to write a for loop which looked like
for (int i=0; i < len; i++) {...}
in groovy you can use that too, but you can use only one count variable. Alternatives to this are
for (i in 0..len-1) {...}or
for (i in 0..<len) {...}or
len.times {...}
Differences from Ruby
The core abstract programming model of Ruby and Groovy are very similar: everything is an object, there is a MOP in control of all activity, and closures are the core structuring tool after classes. Ruby uses the Ruby library, Groovy uses the Java library with some additions of its own. This is the biggest difference but it is a huge difference. Syntactically, things like:
File.open( 'blah' ) { | file | puts( file.read ) }
becomes:
println ( new File ( 'blah' ).text )
which doesn’t show that the Groovy closures syntax is:
{ file -> doSomething ( file ) }
which is slightly different from Ruby, but does show that sometimes Groovy has a different approach to certain things compared to Ruby. So in moving from Ruby to Groovy, there are gotchas.
Some examples and snippets
[wikipedia.org]
[http://dev2dev.bea.com]
[http://groovy.codehaus.org]
Entry filed under: Groovy and Grails, Java, Ruby. Tags: Groovy and Grails, Java, Ruby.







Trackback this post | Subscribe to the comments via RSS Feed