השיטה compare(char x, char y) של מחלקה Character משמשת להשוואה מספרית של שני ערכי char. הערך הסופי שהוחזר דומה למה שיוחזר על ידי:
Character.valueoOf(x).compareTo(Character.valueOf(y))
תחביר
public static intcompare(char x, char y)
פָּרָמֶטֶר
השיטה לעיל דורשת שני פרמטרים:
- char x שהוא התו הראשון להשוואה
- char y שהיא התו השני להשוואה
ערך החזרה
השיטה compare(char x, char y) של מחלקת התווים מחזירה
- ערך 0 אם x==y
- ערך קטן מ-0 אם x
- ערך גדול מ-0 אם x>y.
דוגמה 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.'); } } }בדוק את זה עכשיו
תְפוּקָה:
First value is less than the second value.
דוגמה 2
public class JavaCharacterCompareExample2{ public static void main(String[] args) { char firstValue = '1'; char secondValue = '2'; // 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 = 'J'; char secondVal ='J'; char thirdVal = 'M'; // 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('First and second values are equal.'); } else if (compareOneTwo> 0) { System.out.println('First value is greater than the second value.'); } else { System.out.println('First value is less than the second value.'); } if (compareOneThree == 0) { System.out.println('First and third value are equal.'); } else if (compareOneTwo> 0) { System.out.println('First value is greater than the third value.'); } else { System.out.println('First value is less than the third value.'); } } } </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)>
דוגמה 3
public class JavaCharacterCompareExample3 { public static void main(String[] args) { char firstVal = 'J'; char secondVal ='J'; char thirdVal = 'M'; // 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('First and second values are equal.'); } else if (compareOneTwo> 0) { System.out.println('First value is greater than the second value.'); } else { System.out.println('First value is less than the second value.'); } if (compareOneThree == 0) { System.out.println('First and third value are equal.'); } else if (compareOneTwo> 0) { System.out.println('First value is greater than the third value.'); } else { System.out.println('First value is less than the third value.'); } } }בדוק את זה עכשיו
תְפוּקָה:
First and second values are equal. First value is less than the third value.0)>