SlideShare a Scribd company logo
1 of 24
Download to read offline
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

More Related Content

What's hot

Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Åsa Pehrsson
 
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3mametter
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2Knoldus Inc.
 
Getting started with Flow
Getting started with FlowGetting started with Flow
Getting started with FlowCharlie Dowler
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Divedavanum
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with javaLiviu Tudor
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersRutenis Turcinas
 
Transmogrifier: content migration and time traveling
Transmogrifier: content migration and time travelingTransmogrifier: content migration and time traveling
Transmogrifier: content migration and time travelingJoão Bueno
 
From rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journeyFrom rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journeyArno Schulz
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type scriptDmitrii Stoian
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Ovidiu Farauanu
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaKnoldus Inc.
 
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架jeffz
 

What's hot (20)

Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11Kotlin workshop 2018-06-11
Kotlin workshop 2018-06-11
 
jTransfo quickie at JavaZone 2015
jTransfo quickie at JavaZone 2015jTransfo quickie at JavaZone 2015
jTransfo quickie at JavaZone 2015
 
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3A Static Type Analyzer of Untyped Ruby Code for Ruby 3
A Static Type Analyzer of Untyped Ruby Code for Ruby 3
 
Getting started with typescript and angular 2
Getting started with typescript  and angular 2Getting started with typescript  and angular 2
Getting started with typescript and angular 2
 
Ruby
RubyRuby
Ruby
 
Getting started with Flow
Getting started with FlowGetting started with Flow
Getting started with Flow
 
Oslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep DiveOslo.versioned objects - Deep Dive
Oslo.versioned objects - Deep Dive
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
Fun Ruby
Fun Ruby Fun Ruby
Fun Ruby
 
TypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack DevelopersTypeScript - Silver Bullet for the Full-stack Developers
TypeScript - Silver Bullet for the Full-stack Developers
 
Kotlin from-scratch
Kotlin from-scratchKotlin from-scratch
Kotlin from-scratch
 
Transmogrifier: content migration and time traveling
Transmogrifier: content migration and time travelingTransmogrifier: content migration and time traveling
Transmogrifier: content migration and time traveling
 
From rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journeyFrom rest api to graph ql a 10 year journey
From rest api to graph ql a 10 year journey
 
002. Introducere in type script
002. Introducere in type script002. Introducere in type script
002. Introducere in type script
 
Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)Cap'n Proto (C++ Developer Meetup Iasi)
Cap'n Proto (C++ Developer Meetup Iasi)
 
Shapeless- Generic programming for Scala
Shapeless- Generic programming for ScalaShapeless- Generic programming for Scala
Shapeless- Generic programming for Scala
 
Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!Ruxmon.2013-08.-.CodeBro!
Ruxmon.2013-08.-.CodeBro!
 
Guild Prototype
Guild PrototypeGuild Prototype
Guild Prototype
 
Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015Small Lambda Talk @Booster2015
Small Lambda Talk @Booster2015
 
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架
 

Similar to Introduction to Groovy

Groovy best pratices at EWAY
Groovy best pratices at EWAYGroovy best pratices at EWAY
Groovy best pratices at EWAYĐào Hiệp
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonDomingo Suarez Torres
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneAndres Almiray
 
Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)Joachim Baumann
 
Dsl로 만나는 groovy
Dsl로 만나는 groovyDsl로 만나는 groovy
Dsl로 만나는 groovySeeyoung Chang
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails IntroductionEric Weimer
 
Java Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.EasyJava Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.Easyroialdaag
 
Infinum android talks_10_getting groovy on android
Infinum android talks_10_getting groovy on androidInfinum android talks_10_getting groovy on android
Infinum android talks_10_getting groovy on androidInfinum
 
WeCode IL: Confessions of a java developer that fell in love with the groovy...
WeCode IL:  Confessions of a java developer that fell in love with the groovy...WeCode IL:  Confessions of a java developer that fell in love with the groovy...
WeCode IL: Confessions of a java developer that fell in love with the groovy...Victor Trakhtenberg
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...DevClub_lv
 
Groovy Finesse
Groovy FinesseGroovy Finesse
Groovy Finessemzgubin
 
Groovy Finesse
Groovy FinesseGroovy Finesse
Groovy Finessemzgubin
 
Groovy Up Your Code
Groovy Up Your CodeGroovy Up Your Code
Groovy Up Your CodePaulo Traça
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageHoat Le
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java BasicsFayis-QA
 

Similar to Introduction to Groovy (20)

Groovy best pratices at EWAY
Groovy best pratices at EWAYGroovy best pratices at EWAY
Groovy best pratices at EWAY
 
javerosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparisonjaverosmx-2015-marzo-groovy-java8-comparison
javerosmx-2015-marzo-groovy-java8-comparison
 
Groovy intro
Groovy introGroovy intro
Groovy intro
 
Oscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast LaneOscon Java Testing on the Fast Lane
Oscon Java Testing on the Fast Lane
 
Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)Introduction to Groovy (Serbian Developer Conference 2013)
Introduction to Groovy (Serbian Developer Conference 2013)
 
Dsl로 만나는 groovy
Dsl로 만나는 groovyDsl로 만나는 groovy
Dsl로 만나는 groovy
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
Groovy and noteworthy
Groovy and noteworthyGroovy and noteworthy
Groovy and noteworthy
 
Java Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.EasyJava Edge.2009.Grails.Web.Dev.Made.Easy
Java Edge.2009.Grails.Web.Dev.Made.Easy
 
Polyglot Grails
Polyglot GrailsPolyglot Grails
Polyglot Grails
 
Infinum android talks_10_getting groovy on android
Infinum android talks_10_getting groovy on androidInfinum android talks_10_getting groovy on android
Infinum android talks_10_getting groovy on android
 
WeCode IL: Confessions of a java developer that fell in love with the groovy...
WeCode IL:  Confessions of a java developer that fell in love with the groovy...WeCode IL:  Confessions of a java developer that fell in love with the groovy...
WeCode IL: Confessions of a java developer that fell in love with the groovy...
 
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...Things that every JavaScript developer should know by Rachel Appel at FrontCo...
Things that every JavaScript developer should know by Rachel Appel at FrontCo...
 
Groovy Finesse
Groovy FinesseGroovy Finesse
Groovy Finesse
 
Groovy Finesse
Groovy FinesseGroovy Finesse
Groovy Finesse
 
Java for the Beginners
Java for the BeginnersJava for the Beginners
Java for the Beginners
 
Groovy Up Your Code
Groovy Up Your CodeGroovy Up Your Code
Groovy Up Your Code
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming Language
 
Java script core
Java script coreJava script core
Java script core
 
Core Java Basics
Core Java BasicsCore Java Basics
Core Java Basics
 

Recently uploaded

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...Health
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension AidPhilip Schwarz
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnAmarnathKambale
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsAndolasoft Inc
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxComplianceQuest1
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...ICS
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerThousandEyes
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionSolGuruz
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfkalichargn70th171
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech studentsHimanshiGarg82
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...software pro Development
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...kalichargn70th171
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfproinshot.com
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsArshad QA
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comFatema Valibhai
 

Recently uploaded (20)

+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
+971565801893>>SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHAB...
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
How To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.jsHow To Use Server-Side Rendering with Nuxt.js
How To Use Server-Side Rendering with Nuxt.js
 
A Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docxA Secure and Reliable Document Management System is Essential.docx
A Secure and Reliable Document Management System is Essential.docx
 
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
The Real-World Challenges of Medical Device Cybersecurity- Mitigating Vulnera...
 
Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected WorkerHow To Troubleshoot Collaboration Apps for the Modern Connected Worker
How To Troubleshoot Collaboration Apps for the Modern Connected Worker
 
Diamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with PrecisionDiamond Application Development Crafting Solutions with Precision
Diamond Application Development Crafting Solutions with Precision
 
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdfLearn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
Learn the Fundamentals of XCUITest Framework_ A Beginner's Guide.pdf
 
8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students8257 interfacing 2 in microprocessor for btech students
8257 interfacing 2 in microprocessor for btech students
 
How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...How to Choose the Right Laravel Development Partner in New York City_compress...
How to Choose the Right Laravel Development Partner in New York City_compress...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
The Guide to Integrating Generative AI into Unified Continuous Testing Platfo...
 
Exploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdfExploring the Best Video Editing App.pdf
Exploring the Best Video Editing App.pdf
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
HR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.comHR Software Buyers Guide in 2024 - HRSoftware.com
HR Software Buyers Guide in 2024 - HRSoftware.com
 

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 Everything is 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 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
  • 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