introduction to groovy

24
Introduction to Groovy The next generation JVM programming language

Upload: kevin-tan-hong-ann

Post on 30-Jun-2015

171 views

Category:

Software


1 download

DESCRIPTION

Introduction to Groovy, the next generation JVM programming language.

TRANSCRIPT

Page 1: Introduction to Groovy

Introduction to GroovyThe next generation JVM programming language

Page 2: Introduction to Groovy

What is Groovy?• Scripting language

• Programming Language

• runs on JVM

• dynamic

• derived from Java

• JSR-241

• started from 2003

Page 3: Introduction to Groovy

Problem with Java

• Primitive data types, defies the nature of OO programming

Page 4: Introduction to Groovy

Why Groovy

• Easier than Java syntax

• Fully compatible with Java

• Easy to find workarounds since Java code can be compiled

• Principle of least surprises

Page 5: Introduction to Groovy

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

Page 6: Introduction to Groovy

Groovy FeaturesEverything is an object, including numbers

Example:

5.times{

println “ho”

}

(NOTE: Curly braces is a Closure)

Page 7: Introduction to Groovy

Groovy Features

• Dynamic data type / Explicit typing

def b = 2

def is a reference, asks the compiler to figure out for itself.

Page 8: Introduction to Groovy

Groovy Features

• Objects

new User(name: ‘Kevin’, email:’[email protected]’)

Page 9: Introduction to Groovy

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)

Page 10: Introduction to Groovy

Groovy Features• Collections

Iterator, eg.

r.each { n->

println n

}

or

r.each { println it }

(where ‘it’ is a reserved word)

Page 11: Introduction to Groovy

Groovy Features

• Syntactic sugar for List

def countries = [“Malaysia”, “Thailand”]

Page 12: Introduction to Groovy

Groovy Features• Adding or removing values

list += “Singapore” becomes [“Malaysia”, “Thailand”, “Singapore”]

!

list = list - [“Singapore”] becomes [“Malaysia”, “Thailand”]

Page 13: Introduction to Groovy

Groovy Features

Map

def person = [name:”Kevin”, age:”27”]!

println person.name <- how you access the value

Page 14: Introduction to Groovy

Groovy Features• New for loop syntax

for(i in 1..5){!

println i!

}!

but Java loop syntax is supported anyways.

Page 15: Introduction to Groovy

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

Page 16: Introduction to Groovy

Groovy Features• Switch statement is more general than Java, eg:

switch (val) {

case it > 3:

break

case 5..9:

break

case Number:

break

}

Page 17: Introduction to Groovy

Groovy Features• Groovy Truth

standard conditional operators on boolean expressions

&

boolean expressions for collections (empty collections = false)

Page 18: Introduction to Groovy

Groovy Features• Groovy Truth

Applies for Maps, iterations and enums, objects, numbers

!

http://groovy.codehaus.org/Groovy+Truth

Page 19: Introduction to Groovy

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”

}

Page 20: Introduction to Groovy

Groovy Features• Closures : High Order Functions

with parameters eg.

def methodName = { n, r ->

… do something

}

To call the method,

method(n, r)

Page 21: Introduction to Groovy

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!')

}

}

Page 22: Introduction to Groovy

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}")

}

}

Page 23: Introduction to Groovy

More References

• Groovy http://groovy.codehaus.org

• Grails (www.grails.org) for Web development

• Griffon (www.griffon.org) for desktop app development

Page 24: Introduction to Groovy

Contacts

• http://gplus.to/kevintanhongann