SlideShare uma empresa Scribd logo
1 de 30
Groovy Introducation
Agenda
● What is Groovy?
● History of Groovy.
● Why Groovy?
● Setup Groovy
● What do we mean by Dynamic typing in Groovy?
● Closure in Groovy.
● Comparison between Java and Groovy with examples.
● What is GDK?
● Strings in Groovy:
● Multiline Strings.
● GString.
● Operator overloading in Groovy.
● What is Groovy Truth?
● Groovy Classes
● Working with files.
What is Groovy
Groovy is an object-oriented programming language for
the Java platform. It is a dynamic language with features
similar to those of Python, Ruby, Perl, and Smalltalk. It
can be used as a scripting language for the Java Platform
which provides Dynamic, Easy-to-use and Integration
capabilities to the Java Virutual Machine. It absorbs most
of the syntax from Java and it is much powerful in terms
of funtionalities which is manifiested in the form Closures,
Dynamic Typing etc.
History
●Started by James Strachan and Bob McWhirter in 2003.
●Guillaume Laforge and Jeremy Rainer took it forward.
●Groovy 1.0 release in 2007.
●Now in version 2.4
Why Groovy
● Feels like Java, with no boilerplate code.
● Dynamic.
● Extends JDK.
Setup
● Download the binary from http://groovy.codehaus.org
● Install JDK > 1.5
● Set GROOVY_HOME to point to the installation.
● Add GROOVY_HOME/bin to the path variable.
SDKMAN! (The Software Development Kit
Manager)
● curl -s get.sdkman.io | bash
● source "$HOME/.sdkman/bin/sdkman-init.sh"
● sdk install groovy
● groovy -version
Groovy Shell
Open a terminal window and type “groovysh”.
It allows easy access to evaluate Groovy expressions, and run simple
experiments.
Dynamic Typing Vs Static Typing
First, dynamically-typed languages perform type checking
at runtime, while statically typed languages perform type
checking at compile time.
This means that scripts written in dynamically-typed
languages (like Groovy) can compile even if they contain
errors that will prevent the script from running properly (if
at all). If a script written in a statically-typed language
(such as Java) contains errors, it will fail to compile until
the errors have been fixed.
// Java example
int num;
num = 5;
// Groovy example
num = 5
Groovy is dynamically-typed and determines its variables'
data types based on their values, so this line is not
required.
Closures
● A Closure is a block of code given a name.
● Groovy has support for closures, which work much like Java 8 lambdas. A
closure is an anonymous block of executable code
● Methods can accept closure as parameters.
def helloWorld = {
println "Hello World"
}
Helloworld()
With Parameters
def power = { int x, int y ->
return Math.pow(x, y)
}
println power(2, 3)
Type definition of parameters is the same like variables. If
you define a type you can only use this type, but you can
also skip the type of parameters and pass in anything you
want
def say = { what ->
println what
}
say "Hello World"
Passing Closure
The power of being able to assign closures to variable is
that you can also pass them around to methods.
def transform = { str, transformation ->
transformation(str)
}
println transform("Hello World", { it.toUpperCase() })
Java to Groovy
public class Demo
{
public static void main(String[] args)
{
for(int i = 0; i < 3; i++)
{
System.out.print("shipra" );
}
}
}
Java to Groovy
3.times { print 'Nexthoughts' }
GDK
● Enhancement over JDK.
● The GDK sits on top of the JDK
● Provides new Libraries and APIs to Groovy Developer.
Strings
In groovy , a string can be defined three different ways :
using double quotes, single quotes, or slashes (called
“slashy strings”).
def helloChris = "Hello"
def helloJoseph = 'Hello, World'
def helloJim = /Hello, World/
MultiLine String
● A multiline string is defined by using three double quotes or three single quotes.
● Multiline string support is very useful for creating templates or embedded
documents (such as XML templates, HTML, and so on).
def multiLineString = """
Hello,
This is a multiline string ......
"""
GString
A GString is just like a normal string, except that it
evaluates expressions that are embedded within the
string,in the form ${...}.
def name = "Jim"
def helloName = "Hello, ${name}"
println helloName // Hello, Jim
println helloName.class.name //org.codehaus.groovy.runtime.GStringImpl
Operator Overloading
Groovy supports operator overloading which makes working with Numbers,
Collections, Maps and various other data structures easier to use.
def date = new Date()
date++
println date
All operators in Groovy are method calls.
The following few of the operators supported in Groovy and the
methods they map to
a + b a.plus(b)
a - b a.minus(b)
a * b a.multiply(b)
a ** b a.power(b)
a / b a.div(b)
a % b a.mod(b)
Groovy Truth
In Groovy you can use objects in if and while expressions.
A non-null and non-empty string will evaluate to true.
If("John" ) // any non-empty string is true
if(null) // null is false
if("" ) // empty strings are false
Non zero numbers will evaluate to true.
If(1) // any non-zero value is true
If(-1) // any non-zero value is true
If(0) // zero value is false
Groovy Truth For Collection
A non-empty collection will evaluate to true.
List family = ["John" , "Jane" ]
if(family) // true since the list is populated.
And Empty Collection will evaluate to false
List family = [ ]
if(family) // false since the map is not populated.
Groovy Classes
In Groovy Classes by default things are public unless you specify otherwise.
Class Person {
String name
Integer age
}
Person person = new Person()
person.name = “Per
person.age =30
If you call person.name=”Groovy”
Then behind the scene
person.setName(“Groovy”)
If you want accidental modification in Groovy, you can add a pair of getters and
setters.
Constructors
Person person = new Person(name:”Groovy”, age:20)
Person x = new Person()
Working with Files
new File("foo.txt").bytes
new File("foo.txt").readLines()
new File("foo.txt").eachLine { line -> println(line) }
Thank You
More information please contact at shipra@nexthoughts.com

Mais conteúdo relacionado

Mais procurados

java 8 new features
java 8 new features java 8 new features
java 8 new features Rohit Verma
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11 Knoldus Inc.
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationAndrew Rota
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentationManav Prasad
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressionsLogan Chien
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?Doug Hawkins
 
Collections in Java
Collections in JavaCollections in Java
Collections in JavaKhasim Cise
 
Oracle 資料庫檔案介紹
Oracle 資料庫檔案介紹Oracle 資料庫檔案介紹
Oracle 資料庫檔案介紹Chien Chung Shen
 
Java SE 8 技術手冊第 1 章 - Java平台概論
Java SE 8 技術手冊第 1 章 - Java平台概論Java SE 8 技術手冊第 1 章 - Java平台概論
Java SE 8 技術手冊第 1 章 - Java平台概論Justin Lin
 
JAVA DUMP SET.pdf
JAVA DUMP SET.pdfJAVA DUMP SET.pdf
JAVA DUMP SET.pdfanandn24
 

Mais procurados (20)

Java exception-handling
Java exception-handlingJava exception-handling
Java exception-handling
 
java 8 new features
java 8 new features java 8 new features
java 8 new features
 
Optional in Java 8
Optional in Java 8Optional in Java 8
Optional in Java 8
 
Java collections
Java collectionsJava collections
Java collections
 
Introduction to Java 11
Introduction to Java 11 Introduction to Java 11
Introduction to Java 11
 
Integrating React.js Into a PHP Application
Integrating React.js Into a PHP ApplicationIntegrating React.js Into a PHP Application
Integrating React.js Into a PHP Application
 
Groovy presentation
Groovy presentationGroovy presentation
Groovy presentation
 
Java 8 lambda expressions
Java 8 lambda expressionsJava 8 lambda expressions
Java 8 lambda expressions
 
JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?JVM Mechanics: When Does the JVM JIT & Deoptimize?
JVM Mechanics: When Does the JVM JIT & Deoptimize?
 
Java exception handling
Java exception handlingJava exception handling
Java exception handling
 
Oracle 表格介紹
Oracle 表格介紹Oracle 表格介紹
Oracle 表格介紹
 
JPA and Hibernate
JPA and HibernateJPA and Hibernate
JPA and Hibernate
 
HTTP web server 구현
HTTP web server 구현HTTP web server 구현
HTTP web server 구현
 
Introduction to java 8 stream api
Introduction to java 8 stream apiIntroduction to java 8 stream api
Introduction to java 8 stream api
 
Xke spring boot
Xke spring bootXke spring boot
Xke spring boot
 
Java Annotations
Java AnnotationsJava Annotations
Java Annotations
 
Collections in Java
Collections in JavaCollections in Java
Collections in Java
 
Oracle 資料庫檔案介紹
Oracle 資料庫檔案介紹Oracle 資料庫檔案介紹
Oracle 資料庫檔案介紹
 
Java SE 8 技術手冊第 1 章 - Java平台概論
Java SE 8 技術手冊第 1 章 - Java平台概論Java SE 8 技術手冊第 1 章 - Java平台概論
Java SE 8 技術手冊第 1 章 - Java平台概論
 
JAVA DUMP SET.pdf
JAVA DUMP SET.pdfJAVA DUMP SET.pdf
JAVA DUMP SET.pdf
 

Destaque (20)

Groovy
GroovyGroovy
Groovy
 
Meta Programming in Groovy
Meta Programming in GroovyMeta Programming in Groovy
Meta Programming in Groovy
 
Docker
DockerDocker
Docker
 
Bootcamp linux commands
Bootcamp linux commandsBootcamp linux commands
Bootcamp linux commands
 
Introduction to mongo db
Introduction to mongo dbIntroduction to mongo db
Introduction to mongo db
 
Unit test-using-spock in Grails
Unit test-using-spock in GrailsUnit test-using-spock in Grails
Unit test-using-spock in Grails
 
Actors model in gpars
Actors model in gparsActors model in gpars
Actors model in gpars
 
MetaProgramming with Groovy
MetaProgramming with GroovyMetaProgramming with Groovy
MetaProgramming with Groovy
 
Java reflection
Java reflectionJava reflection
Java reflection
 
Grails services
Grails servicesGrails services
Grails services
 
Grails Controllers
Grails ControllersGrails Controllers
Grails Controllers
 
Groovy DSL
Groovy DSLGroovy DSL
Groovy DSL
 
Grails with swagger
Grails with swaggerGrails with swagger
Grails with swagger
 
Grails domain classes
Grails domain classesGrails domain classes
Grails domain classes
 
Command objects
Command objectsCommand objects
Command objects
 
Jmh
JmhJmh
Jmh
 
RESTEasy
RESTEasyRESTEasy
RESTEasy
 
Reactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJavaReactive java - Reactive Programming + RxJava
Reactive java - Reactive Programming + RxJava
 
Introduction to thymeleaf
Introduction to thymeleafIntroduction to thymeleaf
Introduction to thymeleaf
 
Apache tika
Apache tikaApache tika
Apache tika
 

Semelhante a Groovy intro

An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersKostas Saidis
 
Eclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyEclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyAndres Almiray
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails IntroductionEric Weimer
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageHoat Le
 
Groovy best pratices at EWAY
Groovy best pratices at EWAYGroovy best pratices at EWAY
Groovy best pratices at EWAYĐào Hiệp
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with javaLiviu Tudor
 
Startup groovysession1
Startup groovysession1Startup groovysession1
Startup groovysession1kyon mm
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java DevelopersAndres Almiray
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGuillaume Laforge
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshopadam1davis
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for JavaCharles Anderson
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Wsloffenauer
 
Introduction To Groovy
Introduction To GroovyIntroduction To Groovy
Introduction To Groovymanishkp84
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyAndres Almiray
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to GroovyKevin H.A. Tan
 

Semelhante a Groovy intro (20)

An Introduction to Groovy for Java Developers
An Introduction to Groovy for Java DevelopersAn Introduction to Groovy for Java Developers
An Introduction to Groovy for Java Developers
 
Eclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To GroovyEclipsecon08 Introduction To Groovy
Eclipsecon08 Introduction To Groovy
 
Groovy And Grails Introduction
Groovy And Grails IntroductionGroovy And Grails Introduction
Groovy And Grails Introduction
 
eXo EC - Groovy Programming Language
eXo EC - Groovy Programming LanguageeXo EC - Groovy Programming Language
eXo EC - Groovy Programming Language
 
Groovy best pratices at EWAY
Groovy best pratices at EWAYGroovy best pratices at EWAY
Groovy best pratices at EWAY
 
Groovy features
Groovy featuresGroovy features
Groovy features
 
Groovy & Grails
Groovy & GrailsGroovy & Grails
Groovy & Grails
 
Groovy / comparison with java
Groovy / comparison with javaGroovy / comparison with java
Groovy / comparison with java
 
Startup groovysession1
Startup groovysession1Startup groovysession1
Startup groovysession1
 
Groovy for Java Developers
Groovy for Java DevelopersGroovy for Java Developers
Groovy for Java Developers
 
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume LaforgeGroovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
Groovy and Grails in Action - Devoxx 2008 - University - Guillaume Laforge
 
Learning groovy -EU workshop
Learning groovy  -EU workshopLearning groovy  -EU workshop
Learning groovy -EU workshop
 
Groovy a Scripting Language for Java
Groovy a Scripting Language for JavaGroovy a Scripting Language for Java
Groovy a Scripting Language for Java
 
Svcc Groovy Testing
Svcc Groovy TestingSvcc Groovy Testing
Svcc Groovy Testing
 
2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws2007 09 10 Fzi Training Groovy Grails V Ws
2007 09 10 Fzi Training Groovy Grails V Ws
 
Introduction To Groovy
Introduction To GroovyIntroduction To Groovy
Introduction To Groovy
 
What's New in Groovy 1.6?
What's New in Groovy 1.6?What's New in Groovy 1.6?
What's New in Groovy 1.6?
 
Groovy And Grails
Groovy And GrailsGroovy And Grails
Groovy And Grails
 
GTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with GroovyGTAC Boosting your Testing Productivity with Groovy
GTAC Boosting your Testing Productivity with Groovy
 
Introduction to Groovy
Introduction to GroovyIntroduction to Groovy
Introduction to Groovy
 

Mais de NexThoughts Technologies (20)

Alexa skill
Alexa skillAlexa skill
Alexa skill
 
GraalVM
GraalVMGraalVM
GraalVM
 
Docker & kubernetes
Docker & kubernetesDocker & kubernetes
Docker & kubernetes
 
Apache commons
Apache commonsApache commons
Apache commons
 
HazelCast
HazelCastHazelCast
HazelCast
 
MySQL Pro
MySQL ProMySQL Pro
MySQL Pro
 
Microservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & ReduxMicroservice Architecture using Spring Boot with React & Redux
Microservice Architecture using Spring Boot with React & Redux
 
Swagger
SwaggerSwagger
Swagger
 
Solid Principles
Solid PrinciplesSolid Principles
Solid Principles
 
Arango DB
Arango DBArango DB
Arango DB
 
Jython
JythonJython
Jython
 
Introduction to TypeScript
Introduction to TypeScriptIntroduction to TypeScript
Introduction to TypeScript
 
Smart Contract samples
Smart Contract samplesSmart Contract samples
Smart Contract samples
 
My Doc of geth
My Doc of gethMy Doc of geth
My Doc of geth
 
Geth important commands
Geth important commandsGeth important commands
Geth important commands
 
Ethereum genesis
Ethereum genesisEthereum genesis
Ethereum genesis
 
Ethereum
EthereumEthereum
Ethereum
 
Springboot Microservices
Springboot MicroservicesSpringboot Microservices
Springboot Microservices
 
An Introduction to Redux
An Introduction to ReduxAn Introduction to Redux
An Introduction to Redux
 
Google authentication
Google authenticationGoogle authentication
Google authentication
 

Último

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Hyundai Motor Group
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 3652toLead Limited
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhisoniya singh
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 

Último (20)

CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2Next-generation AAM aircraft unveiled by Supernal, S-A2
Next-generation AAM aircraft unveiled by Supernal, S-A2
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
Tech-Forward - Achieving Business Readiness For Copilot in Microsoft 365
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | DelhiFULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
FULL ENJOY 🔝 8264348440 🔝 Call Girls in Diplomatic Enclave | Delhi
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 

Groovy intro

  • 2. Agenda ● What is Groovy? ● History of Groovy. ● Why Groovy? ● Setup Groovy ● What do we mean by Dynamic typing in Groovy? ● Closure in Groovy. ● Comparison between Java and Groovy with examples. ● What is GDK?
  • 3. ● Strings in Groovy: ● Multiline Strings. ● GString. ● Operator overloading in Groovy. ● What is Groovy Truth? ● Groovy Classes ● Working with files.
  • 4. What is Groovy Groovy is an object-oriented programming language for the Java platform. It is a dynamic language with features similar to those of Python, Ruby, Perl, and Smalltalk. It can be used as a scripting language for the Java Platform which provides Dynamic, Easy-to-use and Integration capabilities to the Java Virutual Machine. It absorbs most of the syntax from Java and it is much powerful in terms of funtionalities which is manifiested in the form Closures, Dynamic Typing etc.
  • 5. History ●Started by James Strachan and Bob McWhirter in 2003. ●Guillaume Laforge and Jeremy Rainer took it forward. ●Groovy 1.0 release in 2007. ●Now in version 2.4
  • 6. Why Groovy ● Feels like Java, with no boilerplate code. ● Dynamic. ● Extends JDK.
  • 7. Setup ● Download the binary from http://groovy.codehaus.org ● Install JDK > 1.5 ● Set GROOVY_HOME to point to the installation. ● Add GROOVY_HOME/bin to the path variable.
  • 8. SDKMAN! (The Software Development Kit Manager) ● curl -s get.sdkman.io | bash ● source "$HOME/.sdkman/bin/sdkman-init.sh" ● sdk install groovy ● groovy -version
  • 9. Groovy Shell Open a terminal window and type “groovysh”. It allows easy access to evaluate Groovy expressions, and run simple experiments.
  • 10. Dynamic Typing Vs Static Typing First, dynamically-typed languages perform type checking at runtime, while statically typed languages perform type checking at compile time. This means that scripts written in dynamically-typed languages (like Groovy) can compile even if they contain errors that will prevent the script from running properly (if at all). If a script written in a statically-typed language (such as Java) contains errors, it will fail to compile until the errors have been fixed.
  • 11. // Java example int num; num = 5; // Groovy example num = 5 Groovy is dynamically-typed and determines its variables' data types based on their values, so this line is not required.
  • 12. Closures ● A Closure is a block of code given a name. ● Groovy has support for closures, which work much like Java 8 lambdas. A closure is an anonymous block of executable code ● Methods can accept closure as parameters.
  • 13. def helloWorld = { println "Hello World" } Helloworld() With Parameters def power = { int x, int y -> return Math.pow(x, y) } println power(2, 3)
  • 14. Type definition of parameters is the same like variables. If you define a type you can only use this type, but you can also skip the type of parameters and pass in anything you want def say = { what -> println what } say "Hello World"
  • 15. Passing Closure The power of being able to assign closures to variable is that you can also pass them around to methods. def transform = { str, transformation -> transformation(str) } println transform("Hello World", { it.toUpperCase() })
  • 16. Java to Groovy public class Demo { public static void main(String[] args) { for(int i = 0; i < 3; i++) { System.out.print("shipra" ); } } }
  • 17. Java to Groovy 3.times { print 'Nexthoughts' }
  • 18. GDK ● Enhancement over JDK. ● The GDK sits on top of the JDK ● Provides new Libraries and APIs to Groovy Developer.
  • 19. Strings In groovy , a string can be defined three different ways : using double quotes, single quotes, or slashes (called “slashy strings”). def helloChris = "Hello" def helloJoseph = 'Hello, World' def helloJim = /Hello, World/
  • 20. MultiLine String ● A multiline string is defined by using three double quotes or three single quotes. ● Multiline string support is very useful for creating templates or embedded documents (such as XML templates, HTML, and so on). def multiLineString = """ Hello, This is a multiline string ...... """
  • 21. GString A GString is just like a normal string, except that it evaluates expressions that are embedded within the string,in the form ${...}. def name = "Jim" def helloName = "Hello, ${name}" println helloName // Hello, Jim println helloName.class.name //org.codehaus.groovy.runtime.GStringImpl
  • 22. Operator Overloading Groovy supports operator overloading which makes working with Numbers, Collections, Maps and various other data structures easier to use. def date = new Date() date++ println date All operators in Groovy are method calls.
  • 23. The following few of the operators supported in Groovy and the methods they map to a + b a.plus(b) a - b a.minus(b) a * b a.multiply(b) a ** b a.power(b) a / b a.div(b) a % b a.mod(b)
  • 24. Groovy Truth In Groovy you can use objects in if and while expressions. A non-null and non-empty string will evaluate to true. If("John" ) // any non-empty string is true if(null) // null is false if("" ) // empty strings are false Non zero numbers will evaluate to true. If(1) // any non-zero value is true If(-1) // any non-zero value is true If(0) // zero value is false
  • 25. Groovy Truth For Collection A non-empty collection will evaluate to true. List family = ["John" , "Jane" ] if(family) // true since the list is populated. And Empty Collection will evaluate to false List family = [ ] if(family) // false since the map is not populated.
  • 26. Groovy Classes In Groovy Classes by default things are public unless you specify otherwise. Class Person { String name Integer age } Person person = new Person() person.name = “Per person.age =30
  • 27. If you call person.name=”Groovy” Then behind the scene person.setName(“Groovy”) If you want accidental modification in Groovy, you can add a pair of getters and setters.
  • 28. Constructors Person person = new Person(name:”Groovy”, age:20) Person x = new Person()
  • 29. Working with Files new File("foo.txt").bytes new File("foo.txt").readLines() new File("foo.txt").eachLine { line -> println(line) }
  • 30. Thank You More information please contact at shipra@nexthoughts.com