Java, type char: storing and representing characters

The data type char is used to store one character (the char comes from character), and for this simple function has the size of type short.

Naturally the question comes to that store just one character?
If there are types for storing numbers, strings and texts, what do we do with a character?

Where do we use the char type?

"Press Y for yes or N for no"
"Press any key to continue to ..."

Or in games ... never noticed that in counter-strike that you go to the left side just press one of the arrows (or 'a' or 'd').
And for Mario Bros jump or drop the fire just by pressing on key?

One key, character...
Often we do not need an entire text file or string, but simply a character.


char name_of_char = 'a';

where we replace 'a' by any alphanumeric character, if we have declared so.
Yes, we can declare otherwise. The characters may be represented by whole well.

char letter_J = 74;
char letter_P = 80;

And inside the printf, represent the char with %c for formatting purposes.
We'll print the initials of the site, see:
public class Types {
    public static void main(String[] args){
        char letter_P = 80;
        char letter_J = 74;
        System.out.printf("%c %c\n",letter_P, letter_J);


BUILD SUCCESSFUL (total time: 0 seconds)

But as I guessed that 'J' is 74 and 'P' is 80? What mathematics or magic to know that?

ASCII Table, which are a form of universal representation of characters by numbers, either decimal, hexadecimal or octal.
These numbers are the decimal representation of the characters, check the table below:

In fact, this representation is human. For many types of numbering.
There is extension of this table for the Chinese, Indians and others for people with different characters. And pros computers?
Computer is male, he is everything to numbers, it's all bit!
Representation is human thing.

I mixed everything, get this:
public class Types {
    public static void main(String[] args){
        int  number_J = 74;
        char letter_J = (char) number_J;
        char letter_P = 80;
        char letter_i = 'i';
        System.out.printf("%c%c%c%c%c%c%c%c%c%c%c %c%c%c%c\n", 
                                    letter_P, 'r', 111, 103, 114, 
                                    101,'s', 's', letter_i, 118,
                                    101, letter_J, 97, 118, 97);


We eould not just equate a char to an integer:
letter_J = number_J

So we made a cast: letter_J char = (char) number_J;
Which is nothing more to tell Java to represent the next type in another way.
In case we are saying 'Hey, Java, brother, do not see number_J as char. I know I declared as char, but just this once, ok? '.

To see more numeric characters and spells:

No comments: