my dog learned polymorphism
The moose likes Beginning Java and the fly likes What does this mean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "What does this mean?" Watch "What does this mean?" New topic

What does this mean?

Manuel Diaz
Ranch Hand

Joined: Apr 22, 2005
Posts: 79
I have this variable 'col' integer type. The variable reads a char type and convert it to integer, like this:

Now, my question is. What does the "- 'o'" represent?. Maybe is the way to change from char to integer, but can someone explain me what is it's function?


Note: I love programming.
Timmy Marks
Ranch Hand

Joined: Dec 01, 2003
Posts: 226
The code makes a digit - from 0 to 9 of a char. The character is represented inside the computer as a series of 0s and 1s. Using ASCII as an example: the character '0' is represented as 48 (decimal), and as with all character sets in use, the digits are arranged sequentially - '1' is 49, '2' is 50 ...

when you subtract '0' from it, you get for '0': '0' - '0' which equates to 48 - 48 which is 0; for '1', '1' - '0' which is 49 - 48 --> 1.

Java uses unicode, but the principle is the same, and furthermore the code doesn't rely on a specific character set as long as the digits are represented sequentially.
Paul Sturrock

Joined: Apr 14, 2004
Posts: 10336

Remember chars are really integral values which equal the unicode value that defines the paricular character. So for example this is valid:

As such, you can perform arithmetic operations on characters. So:

will output: "out: �".

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
I agree. Here's the link:
subject: What does this mean?
It's not a secret anymore!