Groovy Operators

Control Statements

Groovy File Handling

Groovy Error & Exceptions

Groovy Multithreading

Groovy Synchronization

Groovy - xxxValue() method



This method takes on the Number as the parameter and returns a primitive type based on the method which is invoked. Following are the list of methods available −

byte byteValue() 
short shortValue() 
int intValue() 
long longValue() 
float floatValue() 
double doubleValue()

Parameters − No parameters required.

Return Value − The return value is the primitive type returned depending on the value function which is called.

Example - Getting primitive values from an Integer

Following is an example of the usage of the method values.

Example.groovy

class Example { 
   static void main(String[] args) {  
      Integer x = 5; 
		
      // Converting the number to double primitive type
      println(x.doubleValue()); 
		
      // Converting the number to byte primitive type 
      println(x.byteValue()); 
		
      // Converting the number to float primitive type 
      println(x.floatValue());
		
      // Converting the number to long primitive type 
      println(x.longValue()); 
		
      // Converting the number to short primitive type 
      println(x.shortValue()); 
		
      // Converting the number to int primitive type 
      println(x.intValue());  
   } 
}

Output

When we run the above program, we will get the following result −

5.0 
5 
5.0 
5 
5 
5 

Example - Getting primitive values from a Double

Following is an example of the usage of the method values.

Example.groovy

class Example { 
   static void main(String[] args) {  
      Double x = 5.0; 
		
      // Converting the number to double primitive type
      println(x.doubleValue()); 
		
      // Converting the number to byte primitive type 
      println(x.byteValue()); 
		
      // Converting the number to float primitive type 
      println(x.floatValue());
		
      // Converting the number to long primitive type 
      println(x.longValue()); 
		
      // Converting the number to short primitive type 
      println(x.shortValue()); 
		
      // Converting the number to int primitive type 
      println(x.intValue());  
   } 
}

Output

When we run the above program, we will get the following result −

5.0 
5 
5.0 
5 
5 
5 

Example - Getting primitive values from a Long

Following is an example of the usage of the method values.

Example.groovy

class Example { 
   static void main(String[] args) {  
      Long x = 5L; 
		
      // Converting the number to double primitive type
      println(x.doubleValue()); 
		
      // Converting the number to byte primitive type 
      println(x.byteValue()); 
		
      // Converting the number to float primitive type 
      println(x.floatValue());
		
      // Converting the number to long primitive type 
      println(x.longValue()); 
		
      // Converting the number to short primitive type 
      println(x.shortValue()); 
		
      // Converting the number to int primitive type 
      println(x.intValue());  
   } 
}

Output

When we run the above program, we will get the following result −

5.0 
5 
5.0 
5 
5 
5 
groovy_numbers.htm
Advertisements