Skip to content
Related Articles

Related Articles

Scala Char compareTo() method with example
  • Last Updated : 03 Nov, 2019

The compareTo() method is utilized to compare the stated character value with the value in the arguments list. It is same as compare method.

Method Definition: def compareTo(arg0: Character): Int

Return Type: It returns Int. Where, if the stated character value is greater than ‘x’ then a positive integer is returned, if its less than ‘x’ then a negative integer is returned and if its equal to ‘x’ then zero is returned.

Example: 1#




// Scala program of compareTo()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying compareTo() method 
        val result = 'a'.compareTo('A')
          
        // Displays output
        println(result)
      
    }
Output:



32

Example: 2#




// Scala program of compareTo()
// method
  
// Creating object
object GfG
  
    // Main method
    def main(args:Array[String])
    {
      
        // Applying compareTo() method
        val result = 'B'.compareTo('B')
          
        // Displays output
        println(result)
      
    }
Output:
0

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 :