Notice that the step '''in bold''' has been added recently.
- Get the latest java distribution from the http://java.sun.com website.
- Run the installer.
- Get a copy of the groovy distribution from the http://groovy.codehaus.org website, and copy it to some place on your hard drive.
- Unzip the groovy archive to some logical place on your harddrive, I have mine in C:\dev\groovy-1.0-jsr-02
- Get a copy of the jTDS database drivers from the http://jtds.sourceforge.net/ website, and extract it into a folder on your drive (mine is C:\dev\jtds-1.1). Note that for this archive you need to create the folder first, because the files in the archive are not nicely organized in a folder.
- Set the JAVA_HOME, GROOVY_HOME and CLASSPATH environment variables (on Windows, follow these steps)
- Open the System control panel
- Click the Advanced tab
- Click the Environment Variables button
- Add a new System variable with the name JAVA_HOME and the value of the directory java was installed in (mine is C:\Program Files\Java\jdk1.5.0_04)
- Add a new System variable with the name GROOVY_HOME and the value of the directory groovy was installed in (mine is C:\dev\groovy-1.0-jsr-02)
- There should be a User variable with the name CLASSPATH, if there isn't create one and give it the value of the path to the jTDS jar file, (mine is C:\dev\jtds-1.1\jtds-1.1.jar)
- Start a command prompt, and type "set" and hit return to see that your environment variables were set correctly.
- Try opening groovyConsole.bat by double clicking on the icon in the bin directory of the Groovy distribution. If it doesn't work, open a command prompt, and change to the bin directory and run it from there to see what the error message is.
In the top part of the window of the groovyConsole, type the following
And then type <CTRL-R>.
Notice that the text gets printed out in the OS console window (the black one behind the groovyConsole window) and the bottom part of the groovyConsole says:
The line starting with "groovy>" is just the text of what the console processed. The "null" is what the expression "evaluated to". Turns out the expression to print out a message doesn't have any "value" so the groovyConsole printed "null".
Next try something with an actual value. Replace the text in the console with:
or your favorite arithmetic expression, and then type <CTRL-R> (I'm going to stop telling you to hit <CTRL-R>, I think you get the idea). Now the "value" printed at the bottom of the groovyConsole has more meaning.
You can assign values to variables for later use. Try the following:
Arrays and Maps
The Groovy language has built-in support for two important data types, arrays and maps. Arrays are used to store ordered collections of data. For example an integer array of your favorite integers might look like this:
You can access a given item in the array with square bracket notation (indexes start at 0):
Should result in this output:
You can get the length of the array with the "size" method:
Should print out:
But generally you shouldn't need the length, because unlike Java, the preferred method to loop over all the elements in an array is to use the "each" method, which is described below in the "Code as Data" section.
Another native data structure is called a map. A map is used to store "associative arrays" or "dictionaries". That is unordered collections of heterogeneous, named data. For example, let's say we wanted to store names with IQ scores we might have:
Note that each of the values stored in the map is of a different type. Brett's is an integer, Pete's is a string, and Andrew's is a floating point number. We can access the values in a map in two main ways:
Should produce the output:
To add data to a map, the syntax is similar to adding values to an array. For example, if Pete re-took the IQ test and got a 3, we might:
Then later when we get the value back out, it will be 3.
should print out 3.
Also as an aside, you can create an empty map or an empty array with the following:
One of the most important features of any programming language is the ability do execute different code under different conditions. The simplest way to do this is to use the '''if''' construct. For example:
Don't worry too much about the first line, it's just some code to determine whether it is currently before noon or after. The rest of the code executes as follows: first it evaluates the expression in the parentheses, then depending on whether the result is '''true''' or '''false''' it executes the first or the second code block. See the section below on boolean expressions.
Note that the "else" block is not required, but the "then" block is:
There is a special data type in most programming languages that is used to represent truth values, '''true''' and '''false'''. The simplest boolean expression are simply those words. Boolean values can be stored in variables, just like any other data type:
A more complex boolean expression uses one of the boolean operators:
Most of those are probably pretty intuitive. The equality operator is '''==''' to distinguish from the assignment operator '''='''. The opposite of equality is the '''!=''' operator, that is "not equal"
So some examples:
Boolean expressions are especially useful when used in conjunction with the '''if''' construct. For example:
An especially useful test is to test whether a variable or expression is null (has no value). For example let's say we want to see whether a given key is in a map:
Generally null is used to indicate the lack of a value in some location.
Debugging and Troubleshooting Tips
- Print out the class of a variable that you're interested in with myVar.getClass(). Then look up the documentation for that class.
- If you're having trouble with a complex expression, pare it down to a simpler expression and evaluate that. Then build up to your more complex expression.
- Try restarting the groovyConsole (this will clear out all the variables so you can start over.
- Look for the topic you're interested in in the http://groovy.codehaus.org/Language+Guide Groovy language guide
- Also see the official http://wiki.codehaus.org/groovy/GroovyTutorial Groovy tutorial