Introduction to Groovy (Part 2)
Groovy Datatypes
Groovy supports all of the basic datatypes that Java supports. So natives such as int, byte, char, long, boolean, and so forth are all present and accounted for. Also classes such as String, Date, etc. are all readily available (some with interesting additions).
Groovy has support for collections and provides very useful shortcuts and additions for using lists and maps. Lists are implemented in Groovy as a java.util.ArrayList, and maps are implemented as java.util.HashMap. You don’t have to use the “new” keyword to create an instance of them.
Lists are created like the following:
[groovy]colors = ["red", "orange", "yellow"]
numbers = [4, 8, 15, 16, 23, 42]
mixed = ["cheese", 4, new Date(), "5"][/groovy]
Notice we did not type the array, nor did we instantiate it the Java way by typing “List colors = new ArrayList();”. By default lists in Groovy are dynamically typed and it knows how to do the right thing based on the data you are passing it. But if you wanted a non-dynamic list, you certainly could. Groovy provides an “as” keyword that makes your list adhere to certain typing conditions.
[groovy]stringList = ["one", "two", "three"] as String[][/groovy]
What would happen if we mixed our data on a non-dynamic list?
[groovy]whoops = ["one", 2, new Date()] as String[]
yikes = ["beef", 3, new Date()] as int[][/groovy]
If you use the first example, whoops, you will find that it seems to work ok–all of those items can be cast as a String. In the second example, however, two of those elements cannot be cast into an Integer so groovy will throw a GroovyCastException. The point of this is to show that you have to be careful with your data when you use a non-dynamic list. It may appear to work on the surface, but it may not be what you intended.
Here is a brief overview of some of the operations you can perform on lists
[groovy]test = [1, 2, 3, 4]
test.get(2) // Gets the element at index 2 (indexes start with 0)
test[2] // Same as above
test.add(5) // Add a new element containing the int 5
test += [6] // Add a new element containing the int 6
test.size() // get the size of the list[/groovy]
Maps are similar to lists, but the data they contain is in a key/value pair. So instead of a numerical index, you can have an object (like a string) that represents the value of the element. Maps are defined using [key:value] and if you are using a string for the key, you don’t have to use quotes around it. Here is an example:
[groovy]myMap = [name:"Tim", age:27, sign:"Aries"][/groovy]
And you have similar methods you can use to access the data:
[groovy]myMap.get(”name”) // Returns “Tim”
myMap.name // Also returns “Tim”
myMap.size() // Gets the size of the map
// Add some data to the map
myMap.put(”some-key”, “someValue”)
// Returns “someValue” - quotes needed because of
// the punctuation in the key
myMap.’some-key’[/groovy]
Looping in Groovy
For the most part, if statements and loops behave the same as they do in Java, so I’m not going to cover them in large detail. If statements, and while loops are exactly the same. Groovy does not have a “do..while” loop so there is no point in covering that. The big difference that you need to understand comes from both the for loop and the switch statement.
The for loop in Groovy is more like the “for-each” loop construct in Java 5. You use it to iterate over elements in a map, list, range, or other type of collection. It is much easier to work with than the C-style for loop. Let’s say you have a map and you want to iterate over the values:
[groovy]map = [name:"Bubba", occupation:"Redneck", talents:"Brain Surgery"]
for (val in map.values()) {
println(val)
}[/groovy]
You can also iterate over ranges if you need something numeric:
[groovy]for (i in 0..9) {
println(i)
}[/groovy]
Another change from Java is the switch statement. It is now versatile and useful to use a switch statement, because you can now use anything for a case:
[groovy]
switch(”Some string”) {
case String.class:
println “I am a String!”
break
case “Some other string”:
println “I won’t get here”
break
default:
println “Default case if no other matches!”
break
}[/groovy]
These are just some of the basic features of Groovy. There is a lot more to learn, and I highly recommend that you check out the documentation on the Groovy homepage if you are seriously interested in learning more about the language.
- Introduction to Groovy (Part 1)
- Introduction to Groovy (Part 2)
- Introduction to Groovy (Part 3)