Groovy Programming with Eclipse - 2
28 Jan 2008
In the current post, I would present how to work with Numbers and variables in Groovy. Before we move into the program, some Groovy fundas.
1. Groovy classes are binary-compatible with Java classes - the Groovy compiler generates the same byte codes as that of javac.
2. Groovy brings uniformity to Java language. Everything in Groovy is an object - including fundamental data-types.
3. Groovy is dynamically typed. For example, having the following statements in the same block would work in Groovy while fails in Java
def x = 10; // integer
x = "bhargav"; // string
Based on the value assigned to the variable, the type of the variable is determined.
4. Groovy provides "Properties" which are already present in C# and VB.NET. They are kind of mixture of both instance fields and methods.
5. Other interesting feature is the presence of data structures like Maps, List as native language constructs. In java we use ArrayList or LinkedList, while in Groovy, its just []. For example,
def names = []; //creates an empty names list.
Well as you have already seen, to create a variable we use the keyword "def" and if the variable name already exists then we can assign the value we wish to straight-away. Its just that simple.
Use the earlier post and create a new Groovy class in Eclipse. Then try to create a variable using "def" as shown.
def name = "Krishna";
Now since the RHS is a string, internally Groovy compiler picks java.lang.String. We can know what class an object belongs to using the ".class" property.
println name.class;
The above statement would now print "java.lang.String". Now try assigning a different value, now lets assign an integer and look at the class.
name = 100;
println name.class;
This would now print "Integer" on the console. Similary, let us create a list and then see what class internally it uses.
def names = ["Bhargava", " loves ", " Programming."];
println names.class;
Guess what this would print? This prints java.util.ArrayList !! So lists of Groovy internally uses "ArrayList". As we can see everything here is a class and its basically appears as top-layer on Java to make programmers lazy. So other language rules/regulations are still applicable to Groovy. For example, you cannot call non-static methods from a static method directly. Now that we are talking about methods, let us write a simple method. This method should now return us what class the argument passed belongs to. Well, for not-so-experienced but good programmers, one question raises. What should be the return type? Well the class property is of type "Class".
So is name.class.class a valid one?? Definitely! it would be java.lang.Class. Now let us get back to a simple program showing methods in Groovy.
class VariablesAndMethods
{
static main(args) //notice with groovy even the main args is not specified as String[]
{
println className(5);
println className("Bhargava");
println className(100.00);
println className(true);
println className(true.class);
}
static Class className(object)
{
object.class;
}
Wait! where is the "return" statement? Well in groovy, there is no need to specify return explictly. Kind of bad! Well atleast for me. Now the output looks like this:
class java.lang.Integer
class java.lang.String
class java.math.BigDecimal
class java.lang.Boolean
class java.lang.Class
Well let me confuse you more. Shown below is a small method. Tell me what it prints.
static increment(value)
{
value++;
}
Well this returns "value" itself. ( Post Increment operator ).
How about this?
static increment(value)
{
value++;
value;
}
If we notice the return value of this method, we can notice that if there are multiple statements which are like "value;"(equi to return value;) then only the last statement would be equivalent to "return value;".
If you already know, in Java 0 is not False and 1 or more is not true. But in Groovy, the old ways are back. Try this statement ( well for simple statements you can use Groovy console. (From rt click on project->Groovy->Groovy Console. Type in a statement and press CTRL + R ).
println 0==false;
When you run the above statement you would end up getting ClassCastException. But try this:
println someFunction();
where someFunction is defined as
Boolean someFunction()
{
return 0; //return zero.
}
This would print "false" and for any +ve number returned, this prints "true". Kind of confusing, isnt it??
Well enough of methods, in the next post I would continue my talk on numbers and we will see how to perform operations on numbers and stuff.