Open In App

Scala Int compareTo() method with example

The compareTo() method is utilized to returns 0 if the first integer value is equal to the second integer value, 1 if the first one is greater than the second one and finally -1 if the first one is less than the second one.
 

Method Definition: (First_Integer_Value).compareTo(Second_Integer_Value) 
Return Type: It returns 0 if the first integer value is equal to the second integer value, 1 if the first one is greater than the second one and finally -1 if the first one is less than the second one.



Example #1: 
 




// Scala program of Int compareTo()
// method
 
// Creating object
object GfG
{
 
    // Main method
    def main(args:Array[String])
    {
     
        // Creating first Int number
        val m1 = 3
         
        // Applying compareTo method
        val result = m1.compareTo(3)
         
        // Displays output
        println(result)
     
    }
}

Output: 

0

 

Example #2: 
 




// Scala program of Int compareTo()
// method
 
// Creating object
object GfG
{
 
    // Main method
    def main(args:Array[String])
    {
     
        // Creating first Int number
        val m1 = 3
         
        // Applying compareTo method
        val result = m1.compareTo(2)
         
        // Displays output
        println(result)
     
    }
}

Output: 
1

 

Example #3: 
 




// Scala program of Int compareTo()
// method
 
// Creating object
object GfG
{
 
    // Main method
    def main(args:Array[String])
    {
     
        // Creating first Int number
        val m1 = 3
         
        // Applying compareTo method
        val result = m1.compareTo(9)
         
        // Displays output
        println(result)
     
    }
}

Output: 
-1

 


Article Tags :