Introduction
Groovy… * is an agile and dynamic language for the Java Virtual Machine * builds upon the strengths of Java but has additional power features inspired by languages like Python, Ruby and Smalltalk * makes modern programming features available to Java developers with almost-zero learning curve * provides the ability to statically type check and statically compile your code for robustness and performance * supports Domain-Specific Languages and other compact syntax so your code becomes easy to read and maintain * makes writing shell and build scripts easy with its powerful processing primitives, OO abilities and an Ant DSL * increases developer productivity by reducing scaffolding code when developing web, GUI, database or console applications * simplifies testing by supporting unit testing and mocking out-of-the-box * seamlessly integrates with all existing Java classes and libraries * compiles straight to Java bytecode so you can use it anywhere you can use Java
1. Groovy Language Specification
1.1. Syntax
This chapter covers the syntax of the Groovy programming language. The grammar of the language derives from the Java grammar, but enhances it with specific constructs for Groovy, and allows certain simplifications.
1.1.1. Comments
Single line comment
Multiline comment
JavaDoc comment
Shebang line
1.1.2. Keywords
1.1.3. Identifiers
Normal identifiers
Identifiers start with a letter, a dollar or an underscore. They cannot start with a number.
A letter can be in the following ranges:
-
a to z (lowercase ascii letter)
-
A to Z (uppercase ascii letter)
-
\u00C0 to \u00D6
-
\u00D8 to \u00F6
-
\u00F8 to \u00FF
-
\u0100 to \uFFFE
Then following characters can contain letters and numbers.
Here are a few examples of valid identifiers (here, variable names):
def name
def item3
def with_underscore
def $dollarStart
But the following ones are invalid identifiers:
def 3tier
def a+b
def a#b
Quoted identifiers
1.1.4. Strings
Single quoted string
Double quoted string
Triple single quoted string
Triple double quoted string
Slashy string
Dollar slashy string
1.1.5. Numbers
Integer literal
Floating point literal
Binary literal
Octal literal
Hexadecimal literal
Underscore in literal
Number type suffixes
1.1.6. Booleans
1.1.7. Arrays
1.1.8. Lists
1.1.9. Maps
String keys
Parenthesized keys
1.2. Semantics
This chapter covers the semantic of the Groovy programming language.
1.2.1. Typing
Optional typing
Static type checking
Static compilation
1.2.2. Statements
Variable definition
Variable assignment
Multiple assignment
Control structures
Conditional structures
Looping structures
Exception handling
try / catch / finally
Multi-catch
Power assertion
Labeled statements
1.2.3. Expressions
GPath expressions
1.2.4. Promotion and coercion
Number promotion
Closure to type coercion
Map to type coercion
String to enum coercion
1.2.5. Optionality
Optional parentheses
Optional semicolons
Optional return keyword
Optional public keyword
1.2.6. The Groovy Truth
Customizing the truth with asBoolean() methods
1.3. Operators
This chapter covers the operators of the Groovy programming language.
1.3.1. Arithmetic operators
1.3.2. Bitwise and logical operators
1.3.3. Conditional operators
Not operator
Ternary operator
Elvis operator
1.3.4. Object operators
Safe navigation operator
Direct field access operator
Method reference operator
1.3.5. Regular expression operators
Pattern operator
Find operator
Match operator
1.3.6. Other operators
Spread operator
Spreading method arguments
Spread list elements
Spread map elements
Range operator
Spaceship operator
Subscript operator
Membership operator
Identity operator
Coercion operator
Diamond operator
Call operator
1.3.7. Operator precedence
1.3.8. Operator overloading
1.4. Program structure
This chapter covers the program structure of the Groovy programming language.
1.4.1. Package name
1.4.2. Imports
Default imports
Simple import
Star import
Static import
Static star import
Import aliasing
1.4.3. Scripts versus classes
1.4.4. Initializers
Static initializers
Instance initializers
1.5. Object orientation
This chapter covers the object orientation of the Groovy programming language.
1.5.1. Types
Primitive types
Class
Normal class
Static class
Inner class
Abstract class
Interface
Annotation
Closure annotation parameters
Meta-annotations
Annotation placement
Constructors
Named argument constructor
Methods
Method definition
Named arguments
Default arguments
Varargs
Method selection algorithm
Exception declaration
Fields and properties
Fields
Properties
Inheritance
Generics
1.6. Closures
This chapter covers Groovy Closures.