I'm probably missing something, but why are the two numeric values equal to -1
?
System.out.println(Character.getNumericValue(Character.MAX_VALUE));
System.out.println(Character.getNumericValue(Character.MIN_VALUE));
Returns:
-1
-1
GetNumericValue(Char)Converts the specified numeric Unicode character to a double-precision floating point number.
If the char variable contains an int value, we can get the int value by calling Character. getNumericValue(char) method. Alternatively, we can use String. valueOf(char) method.
The getNumericValue(int codePoint) method returns the numeric value of the character, as a non-negative int value. This method will return -2 if the character has a numeric value but the value cannot be represented as a non-negative int value. The method will return -1 if the character has no numeric value..
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);
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.
.. 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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With