Sunday, February 9, 2014

Groovy Tutorial 2: Variables




For those new to programming, variables are memory locations we reserve for values we will use. They are like a basket into which we can put items. For java developers, the thing to remember is that groovy is both a scripting and a dynamic language. We shall look into greater detail about dynamic typing.
 
Dynamic typing means that unlike in Java and other statically-type languages where a variable has a specific type such as String and only takes values of that type, Groovy allows you to change the type as well as value of the variable simply by assigning it a value or object reference of a different type. This is possible since everything in Groovy is an object, and inherits the java super class Object. 
Being a scripting language, we need neither a class nor a main method to act as our entry point to the program. Groovy will simply run any expressions you have in a file. At this point, I’m assuming you have downloaded and installed groovy as well as the Java Development Kit (JDK), or its equivalent for your platform.
You can download Groovy for free as well as access its documentation and installation steps for various platforms at Codehaus.
Declaring Variables
Variables are declared using the def keyword. For your first example on declaring variables, start up your favourite text editor or the GroovyConsole and type this code:
def x=35 //age
println x
Save the file as age1.groovy. This creates a groovy script names age1.groovy. Go to the command-line and navigate to the folder containing your script and type
 groovy age1.groovy  
   Output
35
If you are using GroovyConsole, you can simply run the script by going to Script > Run or using the shortcut Ctrl+R without saving the script.

Everything is an Object
Java programmers are prone to think that x is an int. However, in Groovy, there are no primitives. The variable x is of type java.lang.Integer, which is a class, making x an object. (Objects and Classes will be discussed shortly).
To ascertain this, add another line to age so it now looks like this:
def x=35 //age
println x
println x.getClass() //print x’s class

Output
35
class java.lang.Integer

Groovy is Dynamic

As stated earlier, you can change the type of a variable simply by assigning a value of a different kind. To illustrate this, modify the script you created earlier as shown:
def x=35 //age
println x
println x.getClass() //print x’s class
x="Cabee's Grovy Mastery Course is Awesome"
println x
println x.getClass()
Output
2
class java.lang.Integer
Cabee's Grovy Mastery Course is Awesome
class java.lang.String
You can force a variable to take only values of a given type by explicitly providing the data type of the variable. 
Replace def with Integer and run the script. It will result in a GroovyCastException after printing class java.lang.Integer (Groovy is interpreted).
For more on variables, types, classes, objects and typing, check out the coming tutorials.
Happy coding with Cabee Grovy Mastery Course!!!