5. What is groovy?
Groovy is
A feature-rich
Java-friendly
Dynamic language *
For the Java platform
=> A super version of Java
6. What is a dynamic language?
A language that allows the types of variables to be changed at
runtime...
...among other things!
Difference between dynamic vs static language?
7. Groovy History
• 2003: Started by James Strachan and Bob McWhirter.
• 2004: Commissioned into JSR 241 but was almost abandoned.
• 2005: Brought back to life by Guillaume Laforge and Jeremy
• Rayner.
• 2007: Groovy 1.0.
• 2012: Groovy 2.0.
• 2014: Groovy 2.3 (official support for JDK 8)
• Now : Groovy 2.5 (Stable version)
13. Groovy Features
• Closures
• Collection literals
• Properties (GroovyBeans or POGO)
• Multi-line strings
• String interpolation
• Default arguments
• Everything’s an Object (even numbers)
• Operator overloading
• GPath expressions
• Additional operators
• And much much more...
14. Closures are anonymous functions that:
o may accept parameters or return a value,
o can be assigned to variables,
o can be passed as arguments,
Demo
Groovy Closure
15. A both can take params, return value & do something, but...
Lambda expressions:
Language facility for implementing functional interfaces
• Interfaces with a single abstract method.
• Function, Predicate, Supplier, Consumer.
Groovy closures:
• Can be assign as variable
• Can be reference to default param by "it"
• Use like Functional Programming
Groovy Closure <> Java 8 Lambda
17. "def" keyword
Người đọc không hiểu dữ liệu trả về.
(Đặc biệt với đoạn dài)
=> Gây khó khi đọc hiểu & maintain
18. Don't use "if" with Object
Groovy truth and equals
• Null value is false.
• Empty collection or map is false.
• Empty string is false.
• Zero value is false.
Best pratices:
- Chỉ nên sử dụng với Boolean
- Các trường hợp dùng với Type Number
rất dễ nhầm
- Các trường hợp check List/String nên
check cả null
(StringUtils.notBlank()/CollectionUtils.notEmpty())
20. • GString. (Double quote: "This is GString: ${1 + 1}")
Can be calculate, evaluate method....
• String.
• Single quote: 'This is String'
• Double quote: "This is String" - Like Java String
=> Don't use GString when unnecessary (String constant)
String vs GString