Groovy – Numbers


Groovy – Numbers


”;


In Groovy, Numbers are actually represented as object’s, all of them being an instance of the class Integer. To make an object do something, we need to invoke one of the methods declared in its class.

Groovy supports integer and floating point numbers.

  • An integer is a value that does not include a fraction.
  • A floating-point number is a decimal value that includes a decimal fraction.

An Example of numbers in Groovy is shown below −

Integer x = 5; 
Float y = 1.25; 

Where x is of the type Integer and y is the float.

The reason why numbers in groovy are defined as objects is generally because there are requirements to perform operations on numbers. The concept of providing a class over primitive types is known as wrapper classes.

By default the following wrapper classes are provided in Groovy.

Wrapper Classes

The object of the wrapper class contains or wraps its respective primitive data type. The process of converting a primitive data types into object is called boxing, and this is taken care by the compiler. The process of converting the object back to its corresponding primitive type is called unboxing.

Example

Following is an example of boxing and unboxing −

class Example { 
   static void main(String[] args) {
      Integer x = 5,y = 10,z = 0; 
		
      // The the values of 5,10 and 0 are boxed into Integer types 
      // The values of x and y are unboxed and the addition is performed 
      z = x+y; 
      println(z);
   }
}

The output of the above program would be 15. In the above example, the values of 5, 10, and 0 are first boxed into the Integer variables x, y and z accordingly. And then the when the addition of x and y is performed the values are unboxed from their Integer types.

Number Methods

Since the Numbers in Groovy are represented as classes, following are the list of methods available.

S.No. Methods & Description
1 xxxValue()

This method takes on the Number as the parameter and returns a primitive type based on the method which is invoked.

2 compareTo()

The compareTo method is to use compare one number against another. This is useful if you want to compare the value of numbers.

3 equals()

The method determines whether the Number object that invokes the method is equal to the object that is passed as argument.

4 valueOf()

The valueOf method returns the relevant Number Object holding the value of the argument passed.

5 toString()

The method is used to get a String object representing the value of the Number Object.

6 parseInt()

This method is used to get the primitive data type of a certain String. parseXxx() is a static method and can have one argument or two.

7 abs()

The method gives the absolute value of the argument. The argument can be int, float, long, double, short, byte.

8 ceil()

The method ceil gives the smallest integer that is greater than or equal to the argument.

9 floor()

The method floor gives the largest integer that is less than or equal to the argument.

10 rint()

The method rint returns the integer that is closest in value to the argument.

11 round()

The method round returns the closest long or int, as given by the methods return type.

12 min()

The method gives the smaller of the two arguments. The argument can be int, float, long, double.

13 max()

The method gives the maximum of the two arguments. The argument can be int, float, long, double.

14 exp()

The method returns the base of the natural logarithms, e, to the power of the argument.

15 log()

The method returns the natural logarithm of the argument.

16 pow()

The method returns the value of the first argument raised to the power of the second argument.

17 sqrt()

The method returns the square root of the argument.

18 sin()

The method returns the sine of the specified double value.

19 cos()

The method returns the cosine of the specified double value.

20 tan()

The method returns the tangent of the specified double value.

21 asin()

The method returns the arcsine of the specified double value.

22 acos()

The method returns the arccosine of the specified double value.

23 atan()

The method returns the arctangent of the specified double value.

24 atan2()

The method Converts rectangular coordinates (x, y) to polar coordinate (r, theta) and returns theta.

25 toDegrees()

The method converts the argument value to degrees.

26 radian()

The method converts the argument value to radians.

27 random()

The method is used to generate a random number between 0.0 and 1.0. The range is: 0.0 =< Math.random < 1.0. Different ranges can be achieved by using arithmetic.

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *