Can the char type be categorized as an integer?
Solution 1
Yes, a char
is an integral type in all the popular languages in which it appears. "Integral" means that its spectrum is discrete and the smallest difference between any two distinct values is 1
. The required range of supported values is usually quite small compared to that of other integral types. Computer hardware traditionally treats integers as the fundamental data type; by contrast, arithmetic floating-point types are a more recent and more complicated addition.
Solution 2
Similar to @aioobe's answer
int n = 5;
char ch = (char) '0' + 5; // '5'
or the reverse
char ch = '9';
int i = ch - '0'; // 9
or the unusual
char ch = 'a';
ch += 1; // 'b';
or the odd
char ch = '0';
ch *= 1.1; // '4' as (char) (48 * 1.1) == '4'
ch %= 16; // `\u0004`
int i = ch; // 4
BTW: From String.hashCode()
// int h = 0;
char val[] = value;
int len = count;
for (int i = 0; i < len; i++) {
h = 31*h + val[off++];
}
Solution 3
According to the Java Primitive Data Types tutorial:
char: The char data type is a single 16-bit Unicode character. It has a minimum value of '\u0000' (or 0) and a maximum value of '\uffff' (or 65,535 inclusive).
So yes, it is a 16-bit unsigned integer. Whether you use the type to represent a number or a character is up to you...
Also keep in mind that while the
char
type is guaranteed to be 16 bits in Java, the only restriction C imposes is that the type must be at least 8 bits. According to the C spec reference from this answer:
maximum number of bits for smallest object that is not a bit-field (byte)
CHAR_BIT 8
So a char
in C does not necessarily represent the same range of integer values as a char
in Java.
Solution 4
I'm unsure of the formal definition of an integral type, but in short, yes, char
is an integral type in Java, since it can be seen as representing an integer.
You can for instance do
char c1 = 'a' + 'b';
char c2 = 5;
char c3 = c2 + 3;
int i = c3;
char c4 = (char) i;
and so on.
Solution 5
In memory, essentially everything is integral... but yes. char is an integer type in C, C++ and Java.
Comments
-
Victor S almost 2 years
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?
-
Hans Z almost 12 years
char
in java is defined to be a UTF-16 character, which means it is a 2 byte value somewhere between 0 and 65535. This can be easily interpreted as an integer (the math concept, notint
).char
in c is defined to be a 1 byte character somewhere between 0 and 255. -
Daniel Fischer almost 12 years@HansZ Although
CHAR_BIT == 8
is by far the most common, the C standard allows larger values. There even are a few implementations with 32-bitchar
s, iirc. -
Hans Z almost 12 years@DanielFischer Ansi C standard (C99) $2.2.4.2: * maximum number of bits for smallest object that is not a bit-field (byte) CHAR_BIT 8 * minimum value for an object of type signed char SCHAR_MIN -127 * maximum value for an object of type signed char SCHAR_MAX +127 * maximum value for an object of type unsigned char UCHAR_MAX 255
-
Hans Z almost 12 years@DanielFischer full text: flash-gordon.me.uk/ansi.c.txt
-
Daniel Fischer almost 12 years@HansZ "Their implementation-defined values shall be equal or greater in magnitude (absolute value) to those shown, with the same sign."
-
Hans Z almost 12 years@DanielFischer maximum number of bits for smallest object that is not a bit-field (byte) CHAR_BIT 8
-
Daniel Fischer almost 12 years@HansZ The given value 8 is the minimum allowed value for
CHAR_BIT
. An implementation may give it a greater value.
-
-
Hans Z almost 12 years
char
in java is UTF-16, meaning it is a two byte character.char
in c is defined such thatsizeof(char)
always yields 1, meaning it is necessarily a 1 byte value. -
ouah almost 12 yearsOr a type
T
is an integer type if the value of an object of typeT
is an integer. -
Kerrek SB almost 12 years@ouah: Hm. Is
'a'
an integer? :-) -
Daniel Fischer almost 12 years@KerrekSB In C, it's even an
int
. -
ouah almost 12 years@KerrekSB (C99, 6.4.4.4p10) "The value of an integer character constant containing a single character that maps to a single-byte execution character is the numerical value of the representation of the mapped character interpreted as an integer." It stands OK with my definition ;)
-
noisesmith over 10 yearsUTF-16 is only two bytes on machines where a byte is 8 bits. Byte is architecture dependent.
-
phuclv over 10 yearsno, char in Pascal, which is also a popular language (although not common like the old days), is not an integral type. You'll have to use ord() and chr() to convert between characters and its value
-
Kerrek SB over 10 years@LưuVĩnhPhúc: OK -- but it's still discrete, although it isn't arithmetic, and so the notion of "difference between two values" may not be defined...
-
Eduardo Sebastian about 3 yearsDont except 1$ because char cant be add up