Groovy is a dynamic language for the Java platform that aims to increase developer productivity. It allows optional typing as well as static and dynamic features. Groovy 2.4 requires Java 6+ and everything in Groovy is an object with methods and properties. Common data structures like lists and maps are built into the language to simplify programming tasks.
2. 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.
Groovy is a powerful, optionally typed and
dynamic language, with static-typing and static
compilation capabilities, for the Java platform
aimed at multiplying developers’ productivity
thanks to a concise, familiar and easy to learn
syntax.
3. Groovy 2.4
Groovy 2.4 is the latest version of Groovy.
Important: Releases before 2.4.4 weren't done
under the Apache Software Foundation.
Groovy 2.4 requires Java 6+ with full support up
to Java 8.There are currently some known issues
for some aspects when using Java 9 snapshots.
The groovy-nio module requires Java 7+. Using
Groovy’s invokeDynamic features require Java
7+ but we recommend Java 8.
4. class Example {
static void main(String[] args) {
// Using a simple println statement to print
output to the console
println('HelloWorld');
}
}
5. In Groovy, everything is an object. Objects
have methods and properties.
Methods are the things the object can do, and similar
to other languages, are optionally (more on that later)
invoked with parentheses () that may contain
arguments.
// calling method doSomething on someObject
someObject.doSomething() // calling method
doSomethingElse with one argument
someObject.doSomethingElse("a string argument") //
get the property named someProperty on someObject
someObject.someProperty
6. Strings can be defined using single, double, or
triple quotes:
def a = "some string"
def b = 'another string'
dev c = '''Triple quotes allow multiple lines''‘
Strings defined with double quotes support
interpolation.This allows us to substitute any
Groovy expression into a String at the specified
location. Interpolation is achieved using
the ${} syntax:
7. // simple list of Numbers
def myList = [2, 3, 5, 8, 13, 21]
// use the << operator to append items to a list
myList << 34
assert myList == [2, 3, 5, 8, 13, 21, 34]
// get elements in a list
// first element is at index 0
assert 8 == myList[3]
// can use negative index to start from the end
assert 21 == myList[-2]
// lists can support different types of data
def myMixedList = [1, "two", true]
8. // simple map of key/value pairs def myMap =
[key1: "value1", key2: "value2"]
// can get value for a key with the "." notation:
assert "value1" == myMap.key1
// can also get the value using subscript
notation: assert "value2" == myMap['key2']
// a list of maps
def listOfMaps = [[key1: "val1", key2: "val2"],
[key1: "another val", key2: "and another"]]
assert "another val" == listOfMaps[1].key1
9. What are AST transformations?
An Abstract SyntaxTree (AST) is a in memory
representation of code as data.An ADT
transformation allows to modify this
representation during compile time.
This is sometimes called compile-time meta
programming.
Groovy provides severalAST transformations
which allows you to reduce the amount of
code you have to write
10. What is the Meta Object Protocol_
The Meta-Object Protocol (MOP) is the
underlying layer in Groovy which allows you
to add methods and properties to an object
at runtime. Using MOP you can methods and
properties at runtime to existing objects.
11. Groovy is based on Java regular expression
support and add the addition support
operators to make the usage of regular
expressions easier.
Groovy adds the Slashy string as String
declaration. Slashy strings are Strings
between two "/" signs.They don't need
escape backslashes in regular expressions.
12. Simplification - Groovy does not require semicolons at the end of
statements.
The return keyword can be left out, by default Groovy returns the last
expression of the method, top level parentheses can be left out,
the public keyword can be left out, it is the default in Groovy. It also
allows optional typing.
Flexibility - Groovy allows to change classes and methods at runtime, e.g.
if a method is called which does not exist on a class, the class can
intercept this call and react to it.
This allows for example that Groovy provides a very flexible builder
pattern.
Ease of use - Groovy has list, maps and regular expressions directly build
into the language.
Simplification in I/O - parsing and creating XML, JSON and files is very
simple with Groovy.
13. Groovy automatically imports the following
packages and classes which can be used in
Groovy without specifying the package name.
groovy.lang.*
groovy.util.*
java.lang.*
java.util.*
java.net.*
java.io.*
java.math.BigInteger
java.math.BigDecimal