1. Comparing primitive chars
For comparing char primitives, there are 3 methods, starting off with the easiest approach to the hardest one.
1.1 Using relational operators
This is the easiest way to compare chars in java.
Here java compares the Unicode of the characters and tells if they are the same or not. If the Unicode is same, then the output is true else it’s false.
public static void main(String[] args) { | |
char char1 = 'a'; | |
char char2 = 'b'; | |
// false as 'a' is not equal to 'b' | |
System.out.println(char1 == char2); | |
} |
false
1.2 Using compare() method
This is another way to compare chars in java.
Here we are using the inbuilt compare() method which takes 2 parameters as character 1 and character 2.
This method returns 0 if the characters are same else will give a non-zero value.
public static void main(String[] args) { | |
char char1 = 'a'; | |
char char2 = 'b'; | |
int comparison = Character.compare(char1, char2); | |
if (comparison==0) { | |
System.out.println("the characters are same"); | |
} | |
else { | |
System.out.println("the characters are different"); | |
} | |
} |
the characters are different
1.3 Using Character.hashCode() method
This is a complicated way to compare chars in java.
Here, we get the hashcode of each character using Character.hashCode() method and then check if they are equal or not.
public static void main(String[] args) { | |
char char1 = 'a'; | |
char char2 = 'b'; | |
int char1Code = Character.hashCode(char1); | |
int char2Code = Character.hashCode(char2); | |
if (char1Code == char2Code) { | |
System.out.println("the characters are same"); | |
} | |
else { | |
System.out.println("the characters are different"); | |
} | |
} |
the characters are different
2. Comparing Character objects
For comparing character objects, there are 3 methods, starting with the easiest approach to the hardest one.
2.1 Using equals() method
This is the easiest way to compare character objects in Java.
Here we check if the 2 character objects are equal using the equals() method. This equals() method returns a boolean i.e true or false depending upon if the 2 character objects are equal or not.
public static void main(String[] args) { | |
Character char1 = new Character('a'); | |
Character char2 = new Character('b'); | |
if (char1.equals(char2) == true) { | |
System.out.println("the characters are same"); | |
} else { | |
System.out.println("the characters are different"); | |
} | |
} |
the characters are different
2.2 Using Character.compare() method
This is the second way to compare character objects in Java.
Here we use Character.compare() method to check if the 2 character objects are equal. If the objects are equal, this method returns zero else it returns a non-zero value.
public static void main(String[] args) { | |
Character char1 = new Character('a'); | |
Character char2 = new Character('b'); | |
if (Character.compare(char1, char2) == 0) { | |
System.out.println("the characters are same"); | |
} else { | |
System.out.println("the characters are different"); | |
} | |
} |
the characters are different
2.3 Using Character.hashCode(c) method
This is the third or the final way to compare character objects in Java.
In this case, we use Character.hashCode(c) method which given us the hashcode for each character object and then we check if the hashcode value is the same using if-else.
public static void main(String[] args) { | |
Character char1 = new Character('a'); | |
Character char2 = new Character('b'); | |
int char1Code = Character.hashCode(char1); | |
int char2Code = Character.hashCode(char2); | |
if (char1Code==char2Code) { | |
System.out.println("the characters are same"); | |
} else { | |
System.out.println("the characters are different"); | |
} | |
} |
the characters are different