Java - Character compare() Method



Description

The Java Character compare() method is used to compare two char values numerically and return the numerical difference between these values. The numerical comparison performed by this method is the comparison between corresponding ASCII values of both the char arguments.

For example, let us consider two char values: a and A. When they are both compared numerically, a is greater as its ASCII code is 97 while the ASCII code of A is 65. The result obtained after comparing these values will be 32 (97 65).

This method works identical to the compareTo() method which compares two Character objects numerically, instead of two char values.

Syntax

Following is the syntax of the Java Character compare() method.

public static int compare(char x, char y)

Parameters

  • x − a char value.

  • y − a char value.

Return Value

This method returns 0 if both the char values are equal, a positive difference if x argument is greater than y argument; and a negative difference if x argument is less than y argument.

Comparing two chars Example

The following example shows the usage of Java Character compare() method. We've created two char variables and assigned them two characters. Now using compare() method, we've compared them and result is printed.


public class compare {
   public static void main(String args[]) {
      char x, y;
      int value;
      x = 'a'; // The first argument
      y = 'z'; // The second argument

      // Invoke the method and store the return value in another int variable
      value = Character.compare(x, y);

      // Print the return value
      System.out.println("The value returned after comparison is: " + value);
   }
}

Output

Let us compile and run the program above, to produce the output as follows −

The value returned after comparison is: -25

Comparing two chars Example

But if the first argument passed is greater than the second argument to the method, return value will be the positive difference.


public class compare {
   public static void main(String args[]) {
      char x, y;
      int value;
      x = 'z'; // The first argument
      y = 'a'; // The second argument

      // Invoke the method and store the return value in another int variable
      value = Character.compare(x, y);

      // Print the return value
      System.out.println("The value returned after comparison is: " + value);
   }
}

Output

The output for the program above is produced as follows −

The value returned after comparison is: 25

Comparing two Same chars Example

The following example shows the usage of Java Character compare() method. We've created two char variables and assigned them two characters. Now using compare() method, we've compared them and result is printed.

When we pass numerically equal char values as arguments to the method, the return value obtained will be 0; as there is no numerical difference between these values.


public class compare {
   public static void main(String args[]) {
      char x, y;
      int value;
      x = 'm'; // The first argument
      y = 'm'; // The second argument

      // Invoke the method and store the return value in another int variable
      value = Character.compare(x, y);

      // Print the return value
      System.out.println("The value returned after comparison is: " + value);
   }
}

Output

The output for the program above is produced as follows −

The value returned after comparison is: 0

Comparing two chars and Checking Which char is Greater Example

The following example shows the usage of Java Character compare() method. We've created two char variables and assigned them two characters. Now using compare() method, we've compared them and result is printed.

However, if there is no requirement to print the numerical difference of these values, we can also use conditional statements to only display which value is greater.


public class compare {
   public static void main(String args[]) {
      char x, y;
      int value;
      x = '&';
      y = '+';
      value = Character.compare(x, y);
      if(value > 0)
         System.out.println(x + " is greater than " + y);
      else if(value < 0)
         System.out.println(y + " is greater than " + x);
      else if(value == 0)
         System.out.println("Both values are equal");
   }
}

Output

Compile and run the program above, the output is produced as follows −

+ is greater than &
java_lang_character.htm
Advertisements