Skip to content
Related Articles

Related Articles

Scala Int CompareTo Method With Examples
  • Last Updated : 29 Oct, 2019

compareTo is an int class method in Scala which is used to compare this with that operand. It is similar to the compare Method.

Syntax: (Given_Value1).compareTo(Given_Value2)

Returns: return -1 when Given_Value1 is less than Given_Value2. retun 0 when both values are equal. return 1 when Given_Value1 is greater than Given_Value2.

Example 1:




// Scala Program to demonstrate the 
// compareTo method of Int class
  
object GfG
   
    // Main Method
    def main(args:Array[String])
    {
       
        // Applying the method
        val v1 = (500).compareTo(400)
           
        // Displaying the output
        println(v1)
       
    }
Output:



1

Example 2:




// Scala Program to demonstrate the 
// compareTo method of Int class
  
object GfG
   
    // Main Method
    def main(args:Array[String])
    {
       
        // Applying the method
        val v1 = (200).compareTo(700)
           
        // Displaying the output
        println(v1)
       
    }
Output:
-1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :