Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Introduction to Groovy

Sponsored · Ship Features Fearlessly Turn features on and off without deploys. Used by thousands of Ruby developers. →
Avatar for Kevin Tan Kevin Tan
September 16, 2014

Introduction to Groovy

Introduction to Groovy, the next generation JVM programming language

Avatar for Kevin Tan

Kevin Tan

September 16, 2014

More Decks by Kevin Tan

Other Decks in Programming

Transcript

  1. What is Groovy? • Scripting language • Programming Language •

    runs on JVM • dynamic • derived from Java • JSR-241 • started from 2003
  2. Why Groovy • Easier than Java syntax • Fully compatible

    with Java • Easy to find workarounds since Java code can be compiled • Principle of least surprises
  3. 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
  4. Groovy Features Everything is an object, including numbers Example: 5.times{

    println “ho” } (NOTE: Curly braces is a Closure)
  5. Groovy Features • Dynamic data type / Explicit typing def

    b = 2 def is a reference, asks the compiler to figure out for itself.
  6. 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)
  7. Groovy Features • Collections Iterator, eg. r.each { n-> println

    n } or r.each { println it } (where ‘it’ is a reserved word)
  8. Groovy Features • Adding or removing values list += “Singapore”

    becomes [“Malaysia”, “Thailand”, “Singapore”] ! list = list - [“Singapore”] becomes [“Malaysia”, “Thailand”]
  9. Groovy Features • New for loop syntax for(i in 1..5){!

    println i! }! but Java loop syntax is supported anyways.
  10. 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
  11. Groovy Features • Switch statement is more general than Java,

    eg: switch (val) { case it > 3: break case 5..9: break case Number: break }
  12. Groovy Features • Groovy Truth standard conditional operators on boolean

    expressions & boolean expressions for collections (empty collections = false)
  13. Groovy Features • Groovy Truth Applies for Maps, iterations and

    enums, objects, numbers ! http://groovy.codehaus.org/Groovy+Truth
  14. 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” }
  15. Groovy Features • Closures : High Order Functions with parameters

    eg. def methodName = { n, r -> … do something } To call the method, method(n, r)
  16. 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!') } }
  17. 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}") } }
  18. More References • Groovy http://groovy.codehaus.org • Grails (www.grails.org) for Web

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