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

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...harshavardhanraghave
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)OPEN KNOWLEDGE GmbH
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
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
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyFrank van der Linden
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providermohitmore19
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdfWave PLM
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
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)

Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
Reassessing the Bedrock of Clinical Function Models: An Examination of Large ...
 
Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)Der Spagat zwischen BIAS und FAIRNESS (2024)
Der Spagat zwischen BIAS und FAIRNESS (2024)
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
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
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Engage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The UglyEngage Usergroup 2024 - The Good The Bad_The Ugly
Engage Usergroup 2024 - The Good The Bad_The Ugly
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...Call Girls In Mukherjee Nagar 📱  9999965857  🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
Call Girls In Mukherjee Nagar 📱 9999965857 🤩 Delhi 🫦 HOT AND SEXY VVIP 🍎 SE...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf5 Signs You Need a Fashion PLM Software.pdf
5 Signs You Need a Fashion PLM Software.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
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