Campbell Ritchie wrote:Welcome to JavaRanch Jaka Barrozo.
It's not always a good idea to wake a 3-yr old thread.
What people forget is that a char is a number, not a character. When you write char c = '5'; you do not store a 5 anywhere; you store 53 or more precisely 0000_0000_0011_0101. You don't convert chars to integers at all; chars always are integers (see this Java™ Language Specification section). You convert a character to an integer when you say
char c = '5'
and when you print it out
System.out.println(c);
you are converting an integer to a character. As Justin Fox and Fred Hamilton have shown, you can do arithmetic on chars, but usually you only get sensible answers with addition or subtraction.
wow, three years old, didn't notice that. I was responding to a three year old question.
Anyways, we are getting into some semantic issues here when we speak of conversion
My example was meant as arithemetic, but it was arithemetic
to do a conversion, in use if not in type, in the following sense...
Chess notation is often represented in a string. If I want to parse a character in that string as a row or column co-ordinate in order to access the corresponding array element, well then
that char '5' needs to become an int 5, so I subtract 48 to do this. Not saying it's the best strategy, just a method I adopted some time back when I was learning, and I haven't got around to investigation any better options, yet.