Thursday, October 30, 2008

Division operator in Groovy

Groovy similar to Ruby fairly,in groovy language all of thing is object,that means you have to operate an object by invocation its method.For example 123.abs() will return the absolute value of this integer.Groovy support Integer and Floating point numeric.Indeed,Groovy implement every operator as a method,for example "+" implement as plus:123.plus(456) means 123+456the return value is 578,it's a integer,then on the other hand,for division operator,when you type and run 5/2 the result is 2.5,it's floating point numeric;now how about 6/2,what the result,I think the result is 3,an integer numeric,but I'm wrong,indeed all of the result of division operator is a floating point,even if the result look like an integer.so the result of 6/2 is 3.0,it's a floating point numeric.
And if you want to obtain an integer you should invoke the method intdiv:6.intdiv(2) then the result is an integer:3