Groovy Programming with Eclipse - 3
28 Jan 2008
Let's continue our talk on Numbers and operations.
Arithmetic Operators
1. Addition (+) Operator
2. Subtraction (-) Operator
3. Multiplication (*) Operator
4. Division (/) Operator ( always returns a floating value )
5. Modulus (%) Operator
Example for each of this is shown below.
def num1 = 22;
def num2 = 11;
def add = 22+11; // alternative: 22.plus(11);
def sub = 22-11; // alternative: 22.minus(11);
def mul = 22*11; // alternative: 22.multiply(11);
def div = 22/11; //alternative: 22.div(11);
def rem = 22 % 11; //alternative: 22.mod(11);
Note that div holds 2.0 but not 2 as you expect. If you want integral division, then you should do it as "22.intdiv(11)". intdiv() returns an Integer while a div or / returns a BigDecimal.
The same set of operators holds good for all types of numeric values - integral and non-integral.
Try this : 22.2 % 11; what do you expect would be returned. Well, this is an illegal operation. Since numbers are also objects, the operators +,-,*,etc work because they are "overloaded". Note that multiplicative operators have higher precedence over additive operators(+,-). Also, boolean operators(the comparison operators) have lower precendence over any arithmetic operators.
Other operators of interest are increment and decrement operators. Unary operators are available to increment/decrement a number by 1. For example,
def two = 3-1;
The above statement is perfectly valid. But most of us are already used to do the same using ++ unary operator, which I prefer over anything else if the increment/decrement is by 1. Most of us already know how ++ and -- works. To test if you know, try to guess the output of the simple program shown below:
def v = 10;
def op1 = v++;
def op2 = ++v;
println(op1);
println(op2);
Answers: 10 and 12.
As mentioned earlier, types are assigned at run-time! This is referred to as "dynamic-typing" and this is what makes Groovy a loosely-typed language.
As I mentioned earlier, numbers are objects too! So we have to deal with references. Consider the following snippet.
def num = 12;
def num2 = num;
num++; //will this reflect on num2 too???
println num;
println num2; //guess the output.
it prints 12 and 13. But why?? Both the identifiers refer to the same object. So if one object is modified then the other should be effected too. Isn't it? But the output tells something different The output infact makes sense. When you perform changes on the object itself, then a new object would be created and referred by that identifier. To understand my bad english, try the following code:
def num=12;
def num2 = num;
num2.value++;
println num;
println num2;
Now you can expect the value to be 13 in both the cases! Well justified uh?? by using the "value" property, we are working on the same object! Sharing still maintained!
Boolean/Relational/Comparison Operators
Well as usual <,>,<=,>=,==,!=are comparison or relational operators(also includes the equality operators). Alternative way to do the above said operators is to use compareTo() and equals() methods. For example,
def v1 = 22;
def v2 = 21;
v1.compareTo(v2); //is > 0 in this case.(v1-v2 is ofcourse returned ;)
v1.equals(v2); // returns false.
Note: since the language is loosely typed, i prefer to use .class operator to test what functions/operators bind to what class especially when I try out some new methods. And one last thing to remember is that Garbage collector is always active even when working with plain simple numbers.(ofcourse they are objects !).
In the next post, let us talk about Strings in specific.
I hope atleast one in the world other than me will find this tutorial useful. If so, I appreciate your comments.