Can someone please explain to me what is going on here:
char c = '+';
int i = (int)c;
System.out.println("i: " + i + " ch: " + Character.getNumericValue(c));
This prints i: 43 ch:-1
. Does that mean I have to rely on primitive conversions to convert char
to int
? So how can I convert a Character
to Integer
?
Edit: Yes I know Character.getNumericValue
returns -1
if it is not a numeric value and that makes sense to me. The question is: why does doing primitive conversions return 43
?
Edit2: 43
is the ASCII for +
, but I would expect the cast to not succeed just like getNumericValue
did not succeed. Otherwise that means there are two semantic equivalent ways to perform the same operation but with different results?
43
? – MonarchistgetNumericValue
does what you think it does. That is a utility method that converts characters to integers (as in, the character '5' into the integer 5). That is not the same as a cast (which converts the character '5' into the integer 53). – Tatia