Can the char type be categorized as an integer?

Viewed 16968

Just now I read "char is the only unsigned integral primitive type in Java." Does this mean the char is one of the integral types in Java?

Same as in C, recently I have read that C types includes scalar types, function types, union types, aggregate types, and scalar types include pointer types and arithmetic types, then arithmetic types include integral types and floating-point types, the integral types include enumerated types and character types.

Can the char type really be categorized as a integer both in Java and C?

8 Answers

Char is an "Integer Data Type" in C and its related progeny. As per the K&R Book, "By definition, chars are just small integers". They are used for storing 'Character' data.

The ASCII Table lists 128 characters, and each text character corresponds to an integer value.

Char Data Type is a 1 Byte (8 Bits). Therefore, they can store upto 2^8 = 256 different integers. (These 256 different integers correspond to different ASCII or UTF characters.)

For example:

As per ASCII standard, the letter "x" is stored as 01111000 (decimal 120).

for e.g., you can Add a value in a char variable, just like any integer!

int main()
{
    char a='x';
    int b=3+a;
    printf("value is:\n%d",b);
    return 0;
 }

output: b=123 (3 + 120 for the char 'x')

i.e. chars are just numbers (integers: 0-256 (unsigned)).

Yes a char type can be categorized as an integer:

int i = 49;
int k = 36;

System.out.print((char)i + (char)k + "$"); //may expect 1$ but print: 85$
Related