introduction to groovy
DESCRIPTION
Introduction to Groovy, the next generation JVM programming language.TRANSCRIPT
Introduction to GroovyThe 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 FeaturesEverything 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• 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