Over a million developers have joined DZone.

Acquaintance With Groovy

DZone 's Guide to

Acquaintance With Groovy

· Java Zone ·
Free Resource

For any new language that I wish to learn, first I will try out to code sample application that just output the 'Hello World'

Let's see how it can be done in groovy, it is as simple as typing a text Hello World after the 'print' method.

print 'Hello World!!'

You need to download and install anything in your computer just to try out the sample. You can use web console to execute the sample.

Open the web console: http://groovyconsole.appspot.com/

and then type, above example and click execute script, you will the output in output tab of the UI.

To do the same stuff in java, you have to create a class and add the System.out.print("Hello World"); in main method.

After that you have to compile(using javac) and then run using (java). isn't groovy simplified the syntaxt of a java program a lot.

You might have observed that there is no paranthesis and semicolon as they are optional in groovy.

Before exploring further, I would like to mention that groovy is not a replacement of java, it internally use java and make it more dynamic.

You can mix and match java and groovy code. Your final byte code generated through groovyc(similar to javac) is fully compatible to run with 'java' tool, ensure that %GROOVY_HOME%/embeddable/groovy-all-2.1.jar should be in classpath.

Let's go ahead and see some cool samples I tried out.


This is a simple groovy class helps to generate the XML or html markup.
Let's try out an example to generate the xml file contains list of persons with name and gender

import groovy.xml.MarkupBuilder
def strWriterObj = new StringWriter() 
def xmlObj = new MarkupBuilder(strWriterObj) 
xmlObj.persons() {
    person(gender:'male', 'Alex')  
    person(gender:'male', 'Bob')  
    person(gender:'male', 'Cameron')  
println strWriterObj.toString()
the above code prints the formatted xml output as,
  <person gender='male'>Alex</person>
  <person gender='male'>Bob</person>
  <person gender='male'>Cameron</person>

I guess, you might have noticed the declaration in groovy. You can use 'def' instead of explicity type, groovy will take care of that for you.
Also, you can ignore access specifier as groovy automatically use 'private' for fields and 'public' for all methods.


Parsing and accessing elements in xml is pretty simple in groovy, let's parse the above xml generated using MarkupBuilder.

def sampleXml="""
    <person gender='male'>Alex</person>
    <person gender='male'>Bob</person>
    <person gender='male'>Cameron</person>
def personsNode=new XmlParser().parseText(sampleXml)
//to print all person elements:
    { println "$it" }
//to print first person name
println personsNode.person[0].value
Groovy Templating

Groovy Templating works like a JSP kind of expressions, all works starts with $ will be evaluated at run time and substituted with the property value. 

def textWithPlaceholder='Nice to meet you $title. ${firstname} ${lastname} '
def binding=["title":"Mr", 'firstname':"Alex", 'lastname':"Nancy"]
def engine = new groovy.text.SimpleTemplateEngine() 
def result = engine.createTemplate(textWithPlaceholder).make(binding)
println result.toString()
Strings in groovy

There are 3 types of string syntaxes available in groovy, I will explain these with examples mentioned below.

(1)=>Simple Strings:
String s1='this is simple string'

String s2="this is a GString..evaluates all expressions ${PATH} "

(3)=>Multiline Strings:
String s3="""this is a multiline
 string in groovy"""
IDE and Build tools support

Goovy plugins are available for all popular java IDEs like eclipse, netbeans, Spring Tool Suite(STS) etc.
if you would like to use maven for building groovy code, here is the dependency and plugin usage:



Opinions expressed by DZone contributors are their own.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}