0-All variables in Groovy are reference variables (objects), Groovy does not use primitive variables.
1-A Groovy source files ends with the extension .groovy.
2-All Groovy classes are by default public .
3-In Groovy all fields of a class have by default the access modifier "private"
- Groovy provides automatically getter and setter methods for the fields.
==> all Groovy classes are by default JavaBeans (POJO:Plain Old Java Objects).
4-You can use the getter and setter directly or use the name of the variable for access.
5-Groovy will also directly create constructors in which you can specify the element you would like to set during construction.
Groovy archives this by using the default constructor and then calling the setter methods for the attributes.
6-(==) in Groovy check for equality and not for identity.
7-(is()) to check for identity( Equiv to (==) in java) .
Note : Tow object have some identity if both points to the same object /
8- if after parameter , you add (=0) , this parameter become optional parameter
9-two Methods to loop . (Asseignement Variable , It Variable)
//
using a variable assignment
listName.each
{firstName->
println
firstName
};
//using it variable
listName.each
{println
it};
VARIABLES
10-Groovy allows static and dynamic typed variables. (add def to use dynamic )
Strings
11-Groovy allows to define Strings in '___' and in "____".
=> use "____" to interpret $var as value . if you use '__' , $var will be interpreted as String equals to $var and not (var) value .
=> * type of '____' is java.lang.String
* type of "____" is org.codehaus.groovy.runtime.GStringImpl
Lists and maps
List<Integer> list=[1,4,3,8]
|
*Empty List : =list[]
>>MAP
* Map [key1:value1, key2:value2,...].
*Empty Map =[:]
Map map = [:]
*Empty Map =[:]
Map map = [:]
def map2 = ["key1":"Spring", "key2":"SpringRoo"]
println map2["key1"]
map2["key3"] = "Groovy"
println map2["key3"]
map2["key3"] = "GGTS"
println map2["key3"]
println map2["key4"]
CLOSURE :
Closures are code fragments which can be used without being a method or a class.
{para1, para2 -> code of the closure}
Example: list.each({line -> println line})
Meta Object Protocol
<> to add dynamically at runtime methods and properties .
dsOperators:
Table .
Operator | Name | Method |
---|---|---|
a+b | plus | a.plus(b) |
a-b | minus | a.minus(b) |
a*b | star | a.multiply(b) |
a/b | divide | a.div(b) |
a%b | modulo | a.mod(b) |
a--, --a | decrement | a.previous() |
a++, ++a | increment | a.next() |
a**b | power | a.power(b) |
Safe Navigation Operator
to avoid a NullPointerException
// firstName will be null if user is null // no NPE def firstName = user?.firstName
Elvis operator
short form for the Java ternary operator.
// if user exists, return it, otherwise create a new User
user ?: new User()
Run Groovy
You can run Groovy code via:
- the Groovy shell: groovysh
- the Groovy interpreter: groovy
- the Groovy Console : groovyConsole
- complile Groovy code to classfiles and run it via the Java Virtual machine
To create Java bytecode, run the command
"groovyc Hello.groovy".
"groovyc Hello.groovy".
REFERENCE:
Nice list of features. thanks عبد النور
ReplyDelete