views:

251

answers:

4

I'm probably missing out something, but why the two numeric values are equal to -1?

    System.out.println(Character.getNumericValue(Character.MAX_VALUE));
    System.out.println(Character.getNumericValue(Character.MIN_VALUE));

returns:

-1
-1
+3  A: 

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_VALUEand 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.

Michael Borgwardt
+2  A: 

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.

Kaleb Brasee
Your last sentence is incorrect; there are many more characters that have associated numerical values defined by the Unicode standard.
Michael Borgwardt
Ah, didn't know that. I guess they just give roman numerals as 1 example in the method documentation.
Kaleb Brasee
+2  A: 

.. 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;
Andreas_D
+1  A: 

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);
Alan Moore