Introduction to Groovy 
The next generation JVM programming language
What is Groovy? 
• Scripting language 
• Programming Language 
• runs on JVM 
• dynamic 
• derived from Java 
• JSR-241 
• started from 2003
Problem with Java 
• Primitive data types, defies the nature of OO 
programming
Why Groovy 
• Easier than Java syntax 
• Fully compatible with Java 
• Easy to find workarounds since Java code can be 
compiled 
• Principle of least surprises
Use Cases 
• Scripting instead of Sh, Perl, Python, Ruby, etc 
• Simpler ways of writing unit tests, stub and mocks 
• Configuration/Dependency Management in Gradle 
! 
• Full blown applications via Grails and Griffon
Groovy Features 
Everything is an object, including numbers 
Example: 
5.times{ 
println “ho” 
} 
(NOTE: Curly braces is a Closure)
Groovy Features 
• Dynamic data type / Explicit typing 
def b = 2 
def is a reference, asks the compiler to figure out for 
itself.
Groovy Features 
• Objects 
new User(name: ‘Kevin’, email:’kevin@kevin.com’)
Groovy Features 
• Collections 
New collection type - Range 
Eg. 
1..5 (inclusive, range from 1 to 5) or 
1..<5 (exclusive, range from 1 to 4)
Groovy Features 
• Collections 
Iterator, eg. 
r.each { n-> 
println n 
} 
or 
r.each { println it } 
(where ‘it’ is a reserved word)
Groovy Features 
• Syntactic sugar for List 
def countries = [“Malaysia”, “Thailand”]
Groovy Features 
• Adding or removing values 
list += “Singapore” becomes [“Malaysia”, “Thailand”, 
“Singapore”] 
! 
list = list - [“Singapore”] becomes [“Malaysia”, 
“Thailand”]
Groovy Features 
Map 
def person = [name:”Kevin”, age:”27”]! 
println person.name <- how you access the value
Groovy Features 
• New for loop syntax 
for(i in 1..5){! 
println i! 
}! 
but Java loop syntax is supported anyways.
Groovy Features 
Operator overloading 
a + b (Java) becomes a.plus(b) (Groovy) 
a++ or ++a (Java) becomes a.next() (Groovy) 
switch(a) { case b: } becomes b.isCase(a) 
More info: http://groovy.codehaus.org/Operator+Overloading
Groovy Features 
• Switch statement is more general than Java, eg: 
switch (val) { 
case it > 3: 
break 
case 5..9: 
break 
case Number: 
break 
}
Groovy Features 
• Groovy Truth 
standard conditional operators on boolean 
expressions 
& 
boolean expressions for collections (empty 
collections = false)
Groovy Features 
• Groovy Truth 
Applies for Maps, iterations and enums, objects, 
numbers 
! 
http://groovy.codehaus.org/Groovy+Truth
Groovy Features 
• Closures : High Order Functions 
Idea : Methods are like objects, anonymous class in steroids 
Treated as a “code block” to be executed later, eg. 
def testClosure = { 
println “test closure” 
}
Groovy Features 
• Closures : High Order Functions 
with parameters eg. 
def methodName = { n, r -> 
… do something 
} 
To call the method, 
method(n, r)
Groovy Features 
• File I/O 
def count=0, MAXSIZE=100 
new File("foo.txt").withReader { reader -> 
while (reader.readLine() != null) { 
if (++count > MAXSIZE) throw new RuntimeException('File too 
large!') 
} 
}
Groovy Features 
• File I/O 
def fields = ["a":"1", "b":"2", "c":"3"] 
new File("foo.ini").withWriter { out -> 
fields.each() { key, value -> 
out.writeLine("${key}=${value}") 
} 
}
More References 
• Groovy http://groovy.codehaus.org 
• Grails (www.grails.org) for Web development 
• Griffon (www.griffon.org) for desktop app 
development
Contacts 
• http://gplus.to/kevintanhongann

Introduction to Groovy

  • 1.
    Introduction to Groovy The next generation JVM programming language
  • 2.
    What is Groovy? • Scripting language • Programming Language • runs on JVM • dynamic • derived from Java • JSR-241 • started from 2003
  • 3.
    Problem with Java • Primitive data types, defies the nature of OO programming
  • 4.
    Why Groovy •Easier than Java syntax • Fully compatible with Java • Easy to find workarounds since Java code can be compiled • Principle of least surprises
  • 5.
    Use Cases •Scripting instead of Sh, Perl, Python, Ruby, etc • Simpler ways of writing unit tests, stub and mocks • Configuration/Dependency Management in Gradle ! • Full blown applications via Grails and Griffon
  • 6.
    Groovy Features Everythingis an object, including numbers Example: 5.times{ println “ho” } (NOTE: Curly braces is a Closure)
  • 7.
    Groovy Features •Dynamic data type / Explicit typing def b = 2 def is a reference, asks the compiler to figure out for itself.
  • 8.
    Groovy Features •Objects new User(name: ‘Kevin’, email:’kevin@kevin.com’)
  • 9.
    Groovy Features •Collections New collection type - Range Eg. 1..5 (inclusive, range from 1 to 5) or 1..<5 (exclusive, range from 1 to 4)
  • 10.
    Groovy Features •Collections Iterator, eg. r.each { n-> println n } or r.each { println it } (where ‘it’ is a reserved word)
  • 11.
    Groovy Features •Syntactic sugar for List def countries = [“Malaysia”, “Thailand”]
  • 12.
    Groovy Features •Adding or removing values list += “Singapore” becomes [“Malaysia”, “Thailand”, “Singapore”] ! list = list - [“Singapore”] becomes [“Malaysia”, “Thailand”]
  • 13.
    Groovy Features Map def person = [name:”Kevin”, age:”27”]! println person.name <- how you access the value
  • 14.
    Groovy Features •New for loop syntax for(i in 1..5){! println i! }! but Java loop syntax is supported anyways.
  • 15.
    Groovy Features Operatoroverloading a + b (Java) becomes a.plus(b) (Groovy) a++ or ++a (Java) becomes a.next() (Groovy) switch(a) { case b: } becomes b.isCase(a) More info: http://groovy.codehaus.org/Operator+Overloading
  • 16.
    Groovy Features •Switch statement is more general than Java, eg: switch (val) { case it > 3: break case 5..9: break case Number: break }
  • 17.
    Groovy Features •Groovy Truth standard conditional operators on boolean expressions & boolean expressions for collections (empty collections = false)
  • 18.
    Groovy Features •Groovy Truth Applies for Maps, iterations and enums, objects, numbers ! http://groovy.codehaus.org/Groovy+Truth
  • 19.
    Groovy Features •Closures : High Order Functions Idea : Methods are like objects, anonymous class in steroids Treated as a “code block” to be executed later, eg. def testClosure = { println “test closure” }
  • 20.
    Groovy Features •Closures : High Order Functions with parameters eg. def methodName = { n, r -> … do something } To call the method, method(n, r)
  • 21.
    Groovy Features •File I/O def count=0, MAXSIZE=100 new File("foo.txt").withReader { reader -> while (reader.readLine() != null) { if (++count > MAXSIZE) throw new RuntimeException('File too large!') } }
  • 22.
    Groovy Features •File I/O def fields = ["a":"1", "b":"2", "c":"3"] new File("foo.ini").withWriter { out -> fields.each() { key, value -> out.writeLine("${key}=${value}") } }
  • 23.
    More References •Groovy http://groovy.codehaus.org • Grails (www.grails.org) for Web development • Griffon (www.griffon.org) for desktop app development
  • 24.