- Groovy Tutorial
- Groovy - Home
- Groovy - Overview
- Groovy - Environment
- Groovy - Basic Syntax
- Groovy - Data Types
- Groovy - Variables
- Groovy - Operators
- Groovy - Loops
- Groovy - Decision Making
- Groovy - Methods
- Groovy - File I/O
- Groovy - Optionals
- Groovy - Numbers
- Groovy - Strings
- Groovy - Ranges
- Groovy - Lists
- Groovy - Maps
- Groovy - Dates & Times
- Groovy - Regular Expressions
- Groovy - Exception Handling
- Groovy - Object Oriented
- Groovy - Generics
- Groovy - Traits
- Groovy - Closures
- Groovy - Annotations
- Groovy - XML
- Groovy - JMX
- Groovy - JSON
- Groovy - DSLS
- Groovy - Database
- Groovy - Builders
- Groovy - Command Line
- Groovy - Unit Testing
- Groovy - Template Engines
- Groovy - Meta Object Programming
- Groovy Useful Resources
- Groovy - Quick Guide
- Groovy - Useful Resources
- Groovy - Discussion
Groovy - compareTo()
The compareTo method is to use compare one number against another. This is useful if you want to compare the value of numbers.
Syntax
public int compareTo( NumberSubClass referenceName )
Parameters
referenceName - This could be a Byte, Double, Integer, Float, Long or Short.
Return Value
- If the Integer is equal to the argument then 0 is returned.
- If the Integer is less than the argument then -1 is returned.
- If the Integer is greater than the argument then 1 is returned.
Example
Following is an example of the usage of this method −
class Example { static void main(String[] args) { Integer x = 5; //Comparison against a Integer of lower value System.out.println(x.compareTo(3)); //Comparison against a Integer of equal value System.out.println(x.compareTo(5)); //Comparison against a Integer of higher value System.out.println(x.compareTo(8)); } }
When we run the above program, we will get the following result −
1 0 -1
groovy_numbers.htm
Advertisements