Character.getNumericValue() issue
I'm probably missing something, but why are the two numeric values equal to -1
?
System.out.println(Character.ge开发者_如何学GotNumericValue(Character.MAX_VALUE));
System.out.println(Character.getNumericValue(Character.MIN_VALUE));
Returns:
-1
-1
getNumericValue()
will convert characters that actually represent numbers (like the "normal" digits 0-9, but also numerals in other scripts) to their numeric value. The Characters represented by Character.MAX_VALUE
and Character.MIN_VALUE
do not have such a numeric value; they are not numerals. And according to the API doc:
If the character does not have a numeric value, then -1 is returned.
getNumericValue()
only applies to characters that represent numbers, such as the digits '0'
through '9'
. As a convenience, it also treats the ASCII letters as if they were digits in a base-36 number system (so 'A'
is 10 and 'Z'
is 35).
This one fools a lot of people. If you want to know the Unicode value of a character, all you have to do is cast it to int
:
System.out.println((int)Character.MAX_VALUE);
System.out.println((int)Character.MIN_VALUE);
.. just because \u0000
and '\uffff` don't represent a digit and don't have a numeric value.
I guess you were looking for the 16bit value of the char, but for this we can simply cast:
int value = (int) Character.MAX_VALUE;
Because Character.MAX_VALUE
and Character.MIN_VALUE
aren't numeric. Character.getNumericValue(char) returns -1 when the parameter isn't a character that maps to a number.
Number characters (0-9), letter characters (A-Z), and other unicode number characters are associated with values. I don't know all the other characters that are mapped. But many characters will just return -1.
精彩评论