为什么 Integer.parseInt("\uD835\uDFE8") 会失败?

Why does Integer.parseInt("\uD835\uDFE8") fail?

我的印象是 java 支持 unicode 字符。我做了这个测试,遗憾地发现它失败了。问题是为什么?是错误还是有记录?

// MATHEMATICAL SANS-SERIF ""
String unicodeNum6 = "\uD835\uDFE8";
int codePoint6 = unicodeNum6.codePointAt(0);    
int val6 = Character.getNumericValue(codePoint6);
System.out.println("unicodeNum6 = "+ unicodeNum6
    + ", codePoint6 = "+ codePoint6+ ", val6 = "+val6);
int unicodeNum6Int = Integer.parseInt(unicodeNum6);

这失败了 Exception in thread "main" java.lang.NumberFormatException: For input string: ""

我认为出乎意料,因为 println 工作并打印了预期的行:

unicodeNum6 = , codePoint6 = 120808, val6 = 6

所以Java完全知道unicode字符的数值但是没有在parseInt中使用它。

有人能给出失败的充分理由吗?

这不是错误,行为已记录在案。根据 parseInt(String s, int radix) 的文档(强调我的)

The characters in the string must all be digits of the specified radix (as determined by whether Character.digit(char, int) returns a nonnegative value), except that the first character may be an ASCII minus sign '-' ('\u002D') to indicate a negative value or an ASCII plus sign '+' ('\u002B') to indicate a positive value

如果你尝试:

int aa = Character.digit('\uD835', 10);
int bb = Character.digit('\uDFE8', 10);

你会看到 return -1.
请注意,Integer.parseInt(unicodeNum6); 只会调用 Integer.parseInt(unicodeNum6, 10);