Sendhilkumar Alalasundaram Sendhilkumar Alalasundaram - 11 months ago 55
Java Question

Java: Why String.compareIgnoreCase() uses both Character.toUpperCase() and Character.toLowerCase()?


method of
Class is implemented using the method in the snippet below(jdk1.8.0_45).

i. Why are both
used for comparison? Wouldn't either of them suffice the purpose of comparison?

ii. Why was
not used to implement
? - I understand the same logic can be implemented in different ways. But, still I would like to know if there are specific reasons to choose one over the other.

public int compare(String s1, String s2) {
int n1 = s1.length();
int n2 = s2.length();
int min = Math.min(n1, n2);
for (int i = 0; i < min; i++) {
char c1 = s1.charAt(i);
char c2 = s2.charAt(i);
if (c1 != c2) {
c1 = Character.toUpperCase(c1);
c2 = Character.toUpperCase(c2);
if (c1 != c2) {
c1 = Character.toLowerCase(c1);
c2 = Character.toLowerCase(c2);
if (c1 != c2) {
// No overflow because of numeric promotion
return c1 - c2;
return n1 - n2;

Answer Source

Here's an example using Turkish i's:

System.out.println(Character.toUpperCase('i') == Character.toUpperCase('İ'));
System.out.println(Character.toLowerCase('i') == Character.toLowerCase('İ'));

The first line prints false; the second true. Ideone demo.