Groovy, please try!
For a java programmer sometimes need to write some POC program. The program changed often, sometimes you feel tired to move a lot of interfaces, factories, builders and services. Refactor takes a lot of time and also may create more structure class in your program. Under such situation you may want some dynamic feature for Java, but it is impossible. Groovy was birth for this. I really recommend you to try such programming language.
How to install
For window, you can visit groovy download page to download. For Mac OSX, Linux user better to use SDKMAN to download and configure any Groovy version of your choice. You may also need to install Grails for web development, SDKMAN can also help you to handle it.
Your first program
Please open your favorite editor to input below code and save as MyGroovyScript.groovy
Then in your command type
You will see your screen prints Hello Groovy!
Some String magic
In groovy single quoted strings are plain java.lang.String
object. You can simple use ==
to compare two string’s value are equal. No need to use the String.equals()
method.
Please try to modified the ==
to !=
, you can see a more powerful assert in Groovy which I love the most. As you see there’s no semicolon at the end of the line. This becomes not necessary in Groovy.
Any Groovy expression can be interpolated in all string literals, apart from single and triple single quoted strings. This is a little bit like Perl and very useful when you process some text;
Collections
Groovy also uses some Java collections. Groovy lists are plain JDK java.util.List
the default implementation are java.util.ArrayList
. It can also changed by using as
.
You can fast access a element in a list by its index. If use a negative index, it will count from the backend. Btw also can use a rang to access a list which really readable.
Groovy creates maps are instances of java.util.LinkedHashMap
which a little like Spring map injection.
Closures
A closure in Groovy is an anonymous, block of code that can pass in arguments and return a value. A little complex? Haha, let’s learn it from coding.
With closure we can try functional programming in Groovy.
The nuclear boom: Domain-Specific Language
The most powerful feature in Groovy should be the DSL(Domain-Specific Language) support. Some modern building tools such as Gradle are base on this. With this you can try to build your own language parser. The blow example comes from the official document from groovy-lang, you can find more details from there.
Now we create a DSL with the following structure
Then implementing such a builder is usually done the following way:
We also need to build the spec for a DSL and pass to the builder to execute
Then the type checker will know if an email
method accepting a Closure.
It is magic right?
Some other useful tools
There are some useful tools build on Groovy. Let me take a short tour.
Gradle
Gradle is a modern building tools. Now is the official recommend building tool for Android Developer. Gradle build on Groovy DSL, which is a better solution for construct building steps. To compare with Maven, with a Java plugin in Gradle, the build script can be like this:
Grails
Grails is a powerful web framework which including integrated ORM, Domain-specific Language, runtime and compile-time meta-programming and Asynchronous programming. If you want to develop a web application don’t forget to take a try with this.