Open In App

Scala Int compare() method with example

Improve
Improve
Like Article
Like
Save
Share
Report

The compare() 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).compare(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




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


Output:

0

Example #2: 

Scala




// Scala program of Int compare()
// method
 
// Creating object
object GfG
{
 
    // Main method
    def main(args:Array[String])
    {
     
        // Applying compare method
        val result = (10).compare(5)
         
        // Displays output
        println(result)
     
    }
}


Output:

1

Example #3: 

Scala




// Scala program of Int compare()
// method
 
// Creating object
object GfG
{
 
    // Main method
    def main(args:Array[String])
    {
     
        // Applying compare method
        val result = (10).compare(20)
         
        // Displays output
        println(result)
     
    }
}


Output:

-1


Last Updated : 14 Dec, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads