是什么让 'getCharNumber' 方法不区分大小写,而它只检查小写字母(作者 CtCI)

What makes the 'getCharNumber' method case-insensitive while it only every checks for lowercase (by author of CtCI)

public class Common {

    public static int getCharNumber(Character c) {
        int a = Character.getNumericValue('a');
        int z = Character.getNumericValue('z');
        
        int val = Character.getNumericValue(c);
        if (a <= val && val <= z) {
            return val - a;
        }
        return -1;
    }
    
    public static int[] buildCharFrequencyTable(String phrase) {
        int[] table = new int[Character.getNumericValue('z') - Character.getNumericValue('a') + 1];
        for (char c : phrase.toCharArray()) {
            int x = getCharNumber(c);
            if (x != -1) {
                table[x]++;
            }
        }
        return table;
    }
}

上述算法用于测试一个字符串是否是回文的排列,由 CtCI (Cracking the Coding Interview) 编写。

我的问题:为什么 getCharNumber 方法不区分大小写?

我认为它应该区分大小写,因为它只检查小写字符。

为什么 getCharNumber 不区分大小写?

getCharNumber 方法使用 Java 的 Character#getNumericValue(char) 方法,其 JavaDoc 特别指出:

The letters A-Z in their uppercase ('\u0041' through '\u005A'), lowercase ('\u0061' through '\u007A'), and full width variant ('\uFF21' through '\uFF3A' and '\uFF41' through '\uFF5A') forms have numeric values from 10 through 35. This is independent of the Unicode specification, which does not assign numeric values to these char values.

意味着例如对于字符 Aa 这个 API 方法 returns 相同的值 ,即 10,因此不区分大小写。


供参考,另请参阅

  • Character.getNumericValue(..) in Java returns same number for upper and lower case characters
  • What is the reverse of Character.getNumericValue
  • Java Character literals value with getNumericValue()
  • Character.getNumericValue() issue