Message-ID: <1103654677.577.1425600422536.JavaMail.firstname.lastname@example.org> Subject: Exported From Confluence MIME-Version: 1.0 Content-Type: multipart/related; boundary="----=_Part_576_2011726435.1425600422535" ------=_Part_576_2011726435.1425600422535 Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Content-Location: file:///C:/exported.html
Groovy requires Java, so you need to have a version available (while gro= ovy 1.6 supported JDK 1.4 or greater, for groovy 1.7 onwards, minimum JDK 1= .5 is needed). Here are the steps if you don't already have Java installed:=
Download the Groovy installer or binaries from the downloa= ds page and follow the installation instructions. (There is curre= ntly an issue where you cannot have spaces in the path where Groovy is inst= alled under windows. So, instead of accepting the default installatio= n path of "c:\Program Files\Groovy" you will want to change the p= ath to something like "c:\Groovy")
You may wish to obtain optional jar files, either corresponding to Groov= y modules (see module documentation for details) or corresponding to other = Java classes you wish to make use of from Groovy. Some possibilities are li= sted below:
Database driver for SQL Server and/or Sybase<= /p>
Database driver for HSQLDB, a 100% Java datab= ase
The recommended way for making Groovy be aware of your additional jar fi=
les is to place them in a predefined location. Your Groovy install should i=
nclude a file called
groovy-starter.conf. Within that file, ma=
ke sure a line such as
is not commented out. The
.home system property is set by your operating system. (Mine is
(Note: as an alternative, y= ou can set up a CLASSPATH variable and make sure it mentions all of your ad= ditional jar files, otherwise Groovy works fine with an empty or no CLASSPA= TH variable.)
In the top part of the window of the groovyConsole, type the following= p>
And then type <CTRL-R>.
= Notice that the text gets printed out in the OS console window (the black o= ne behind the groovyConsole window) and the bottom part of the groovyConsol= e says:
The line starting with "groovy&= gt;" is just the text of what the console processed. The "null&qu= ot; is what the expression "evaluated to". Turns out the expressi= on to print out a message doesn't have any "value" so the groovyC= onsole printed "null".
Next try something with an actual va= lue. Replace the text in the console with:
or your favorite arithmetic expressi= on, 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:
The Groovy language has built-in support for two important data types, l= ists and maps (Lists can be operated as arrays in Java language). Lists are= used to store ordered collections of data. For example an integer list of = your favorite integers might look like this:
You can access a given item in the l= ist with square bracket notation (indexes start at 0):
Should result in this output:
You can get the length of the list w= ith 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 ele= ments in an list is to use the "each" method, which is described = below in the "Code as Data" section.
Another native data st= ructure is called a map. A map is used to store "associative arrays&qu= ot; or "dictionaries". That is unordered collections of heterogen= eous, named data. For example, let's say we wanted to store names with IQ s= cores 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 strin= g, and Andrew's is a floating point number. We can access the values in a m= ap in two main ways:
Should produce the output:
To add data to a map, the syntax is = similar to adding values to an list. For example, if Pete re-took the IQ te= st and got a 3, we might:
Then later when we get the value bac= k out, it will be 3.
should print out 3.
Also as an= aside, you can create an empty map or an empty list with the following:
To make sure the lists are empty, yo= u can run the following lines:
Should print a size of 0 for the Lis= t and the Map.
One of the most important features of any programming language is the ab= ility to execute different code under different conditions. The simplest wa= y 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&qu= ot; 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 variabl= es, just like any other data type:
A more complex boolean expression us= es one of the boolean operators:
Most of those are probably pretty in= tuitive. The equality operator is '''=3D=3D''' to distinguish from the assi= gnment operator '''=3D'''. The opposite of equality is the '''!=3D''' opera= tor, that is "not equal"
So some examples:
Boolean expressions are especially u= seful 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 t= he lack of a value in some location.