compare char from 2 strings in java code example
Example 1: compare 2 characters in java
public class JavaCharacterCompareExample1 {
public static void main(String[] args) {
char firstValue = 'A';
char secondValue = 'B';
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.");
}
}
}
Example 2: how to compare a char to a string java
public class JavaCharacterCompareExample1 {
public static void main(String[] args) {
char firstValue = 'A';
char secondValue = 'B';
int result = Character.compare(firstValue, secondValue);
if (result == 0) {
System.out.println("The chars are the same.");
} else {
System.out.println("The chars are different.");
}
}
}