Java - Character compareTo() Method



The Java Character compareTo() method is used to compare two Character objects numerically.

This built-in method in Java Character class accepts a Character Object as an argument and is invoked on another Character Object to be compared with.

If the argument Character is greater than this Character, the method will produce a negative value; but if the argument Character is less than this Character, the method returns a positive value. Zero is produced if both the Objects have same reference.

Note − This is strictly a numerical comparison; it is not locale-dependent.

Syntax

Following is the syntax for Java Character compareTo() method

public int compareTo(Character anotherCharacter)

Parameters

  • anotherCharacter − the Character to be compared.

Return Value

This method returns the value 0 if the argument Character is equal to this Character, a negative value if this Character is numerically less than the Character argument; and a positive value if this Character is numerically greater than the Character argument.

Example

The following example shows the usage of Java Character compareTo() method.

package com.tutorialspoint;
import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 Character objects c1, c2
      Character c1, c2;

      // assign values to c1, c2
      c1 = new Character('a');
      c2 = new Character('b');

      // create an int type
      int res;

      // compare c1 with c2 and assign result to res
      res = c1.compareTo(c2);

      String str1 = "Both values are equal ";
      String str2 = "First character is numerically greater";
      String str3 = "Second character is numerically greater";
      if( res == 0 ) {
         System.out.println( str1 );
      } else if( res > 0 ) {
         System.out.println( str2 );
      } else if( res < 0 ) {
         System.out.println( str3 );
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Second character is numerically greater

Example

In this example, let us instantiate the Character objects with digits as char values. The method is called and these digits are compared.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 Character objects c1, c2
      Character c1, c2;

      // assign values to c1, c2
      c1 = new Character('9');
      c2 = new Character('1');      
      int result;

      // compare c1 with c2 and assign to result
      result = c1.compareTo(c2);

      if( result == 0 ) {
         System.out.println("Both values are equal ");
      } else if( result > 0 ) {
         System.out.println("First character is numerically greater");
      } else if( result < 0 ) {
         System.out.println("Second character is numerically greater");
      }
   }
}

Output

Once we compile and run the code above, the method compares the two input digit characters and the output is printed as −

First character is numerically greater

Example

Following example takes symbols as character objects and compares the Unicode code points of these objects to return the numerically greater symbol.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {

      // create 2 Character objects c1, c2
      Character c1, c2;

      // assign values to c1, c2
      c1 = new Character('*');
      c2 = new Character('/');
      int result;

      // compare c1 with c2 and assign to result
      result = c1.compareTo(c2);

      if( result == 0 ) {
         System.out.println("Both values are equal ");
      } else if( result > 0 ) {
         System.out.println("First character is numerically greater");
      } else if( result < 0 ) {
         System.out.println("Second character is numerically greater");
      }
   }
}

Output

After compiling, the code is executed to achieve the following output −

Second character is numerically greater

Example

We must understand that the Java Character compareTo() method only compares Character Objects, which are totally different from the char data type. However, if the input given to the program is a char variable, a compile time error is prompted.

import java.lang.*;
public class CharacterDemo {
   public static void main(String[] args) {
      char c1, c2;
      c1 = 'A';
      c2 = 'B';
      int result;
      result = c1.compareTo(c2);
      if( result == 0 ) {
         System.out.println("Both values are equal ");
      } else if( result > 0 ) {
         System.out.println("First character is numerically greater");
      } else if( result < 0 ) {
         System.out.println("Second character is numerically greater");
      }
   }
}

Compile Time Error

Executing the above code will result in a compile time error as the char variable cannot reference to a method. It is only possible using Objects of a class the method is present in.

CharacterDemo.java:9: error: char cannot be dereferenced
      result = c1.compareTo(c2);
                 ^
1 error
java_lang_character.htm
Advertisements