logo

Méthode Java Character compare()

La méthode compare(char x, char y) de la classe Character est utilisée pour comparer numériquement deux valeurs de caractères. La valeur finale renvoyée est similaire à celle qui serait renvoyée par :

nœud de liste
 Character.valueoOf(x).compareTo(Character.valueOf(y)) 

Syntaxe

 public static intcompare(char x, char y) 

Paramètre

La méthode ci-dessus nécessite deux paramètres :

  • char x qui est le premier caractère à comparer
  • char y qui est le deuxième caractère à comparer

Valeur de retour

La méthode compare(char x, char y) de la classe de caractères renvoie

  • une valeur 0 si x==y
  • une valeur inférieure à 0 si x
  • une valeur supérieure à 0 si x>y.

Exemple 1

 public class JavaCharacterCompareExample1 { public static void main(String[] args) { char firstValue = 'A'; char secondValue = 'B'; // compare the first char to the second int compareOneTwo = Character.compare(firstValue, secondValue); if (compareOneTwo> 0) { System.out.println('First value is greater than second value'); } else { System.err.println('First value is less than second value.'); } } } 
Testez-le maintenant

Sortir:

 First value is less than the second value. 

Exemple 2

 public class JavaCharacterCompareExample2{ public static void main(String[] args) { char firstValue = &apos;1&apos;; char secondValue = &apos;2&apos;; // compare the first char to the second int comp = Character.compare(firstValue, secondValue); if (comp<0) 1 { system.err.println('value is greater than the value 2.'); } else less second value2.'); < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Value 1 is greater than the value 2. </pre> <h2>Example 3</h2> <pre> public class JavaCharacterCompareExample3 { public static void main(String[] args) { char firstVal = &apos;J&apos;; char secondVal =&apos;J&apos;; char thirdVal = &apos;M&apos;; // compare the first char to the second CHAR int compareOneTwo = Character.compare(firstVal, secondVal); // compare the first char to the third int compareOneThree = Character.compare(firstVal, thirdVal); if (compareOneTwo == 0) { System.out.println(&apos;First and second values are equal.&apos;); } else if (compareOneTwo&gt; 0) { System.out.println(&apos;First value is greater than the second value.&apos;); } else { System.out.println(&apos;First value is less than the second value.&apos;); } if (compareOneThree == 0) { System.out.println(&apos;First and third value are equal.&apos;); } else if (compareOneTwo&gt; 0) { System.out.println(&apos;First value is greater than the third value.&apos;); } else { System.out.println(&apos;First value is less than the third value.&apos;); } } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> First and second values are equal. First value is less than the third value. </pre></0)>

Exemple 3

 public class JavaCharacterCompareExample3 { public static void main(String[] args) { char firstVal = &apos;J&apos;; char secondVal =&apos;J&apos;; char thirdVal = &apos;M&apos;; // compare the first char to the second CHAR int compareOneTwo = Character.compare(firstVal, secondVal); // compare the first char to the third int compareOneThree = Character.compare(firstVal, thirdVal); if (compareOneTwo == 0) { System.out.println(&apos;First and second values are equal.&apos;); } else if (compareOneTwo&gt; 0) { System.out.println(&apos;First value is greater than the second value.&apos;); } else { System.out.println(&apos;First value is less than the second value.&apos;); } if (compareOneThree == 0) { System.out.println(&apos;First and third value are equal.&apos;); } else if (compareOneTwo&gt; 0) { System.out.println(&apos;First value is greater than the third value.&apos;); } else { System.out.println(&apos;First value is less than the third value.&apos;); } } } 
Testez-le maintenant

Sortir:

 First and second values are equal. First value is less than the third value.