Groovy from the ground up
Java's power, with the ceremony left out
- Beginner
- 12 chapters
- 197 min read
Learn Groovy by running it: printing and expressions, def against written types, numbers and text with interpolation, lists and maps, decisions and truthiness, loops over ranges, closures and the list methods built on them, your own methods with defaults, classes with properties and toString, the String methods worth knowing, exceptions you can handle, and a report that sorts, groups and formats a table of records.
What You'll Learn
- Write and run a Groovy script, and know why it needs no class or main method
- Declare values with def or with a written type, and tell which type came out
- Interpolate text with GStrings, and do arithmetic that keeps its fractions
- Keep values in lists and maps, grow them, and nest them into a table
- Branch with if, switch, the ternary and the Elvis operator, and know what is false
- Repeat work with for-in, each, times and while, and leave a loop early
- Write closures, use the implicit it, and replace loops with collect and findAll
- Define methods with defaults, and return the last expression on purpose
- Build classes with properties, constructors and a toString that prints well
- Trim, split, join, pad and format text with String methods and sprintf
- Throw with a reason, catch what you can handle, and clean up in finally
- Sort with a comparator, group records with groupBy, and print a report
Prerequisites
- No programming experience needed — this course starts at the first line
- Knowing any JVM language, or any scripting language, makes the early chapters quick
Course Content
- 01Your first Groovy scriptNot started
Printing a line, letting Groovy do the arithmetic, and the flat shape of a script that has no class and no main method.
- 02Values, def and typesNot started
Two ways to declare a variable — letting the value decide with def, or writing the type down — and what each one buys you when the program grows.
- 03Numbers and text togetherNot started
Quoting that interpolates and quoting that does not, braced expressions inside text, and the arithmetic operators that decide whether you get 2 or 2.5.
- 04Lists and mapsNot started
Square brackets give you both of Groovy's everyday collections: an ordered list you can grow, and a map of names to values that keeps its keys.
- 05DecisionsNot started
Choosing a branch with if and switch, the two shorthands worth knowing — the ternary and the Elvis operator — and the values Groovy treats as false.
- 06Loops and rangesNot started
Repeating work four ways — for-in over a range or list, each over a list, times for a count, while for an unknown number of passes — and how to leave early.
- 07ClosuresNot started
Blocks of code you can name and pass around, the implicit `it`, and the three methods that replace most loops: collect, findAll and find.
- 08Your own methodsNot started
Naming a piece of work: parameters, default arguments that fill in what the caller leaves out, and the last expression that answers for you.
- 09ClassesNot started
A type of your own: properties that come with getters and setters for free, a constructor that fills them in, methods that use them, and a toString that decides what printing means.
- 10Working with textNot started
The String methods you will actually use: trimming, splitting, joining, replacing, padding, and formatting a line so the columns line up.
- 11Errors you can handleNot started
Throwing with a reason, catching only what you can do something about, the finally block that always runs, and the difference between a bug and a bad input.
- 12Sorting, grouping and a reportNot started
The last two closure shapes — a comparator that decides order and groupBy that buckets a table — brought together in a report built from records.
