This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Why does a char value also have an integer value? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why does a char value also have an integer value?" Watch "Why does a char value also have an integer value?" New topic

Why does a char value also have an integer value?

John Quach
Ranch Hand

Joined: Sep 28, 2011
Posts: 50
From a tutorial on making games, this is a ResourceManager class that handles Sprites and map loading. With the help of another class, it loads a text file that uses char values to be parsed into something like a tree, ground, rocks, and character sprites. Each image makes up the entire map.

In the loadmap() method, this is the line of code where I'm confused at.

ch is a char value. But its acting like an int value in that operation. I get that char variables have their int values. I don't know why but that's the fact. What I don't understand is how that statement is used in the "for" loop.

How does help parse each char into a tilemap image?

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Characters are represented on computers as binary values just like integers; these binary values can be treated like integers in some cases. The value "ch - 'A'" is, in this case, being used as the index into an array list of images (tiles.get(value)).

For unicode, letters 'A' through 'Z' have sequential values, and if you subtract 'A' from an uppercase letter, you get a value from 0 to 25. The same is true for Ascii, though Ascii is a 7 or 8 bit code instead of a nominally-16-bit code as unicode is. The 0-25 can then be used as an index into an array or any sequential storage.

It is sorta covered in the JavaRanch Style Guide.
subject: Why does a char value also have an integer value?
Similar Threads
SWT XML Sprite Sheet Utility
Filereader - Reading >1 char at a time
Having problems with Window and Frame
Images on top of each other
Isometric assistance needed