Java Data and Operators

7. Character Data and Operators

7.1. Character to Integer Conversions

Is 'A' a character or an integer? The fact that character data are stored as integers in the computer's memory can cause some confusion about whether a given piece of data is a character or an integer. In other words, when is a character, for example 'A', treated as the integer (65) instead of as the character 'A'? The rule in Java is that a character literal – 'a' or 'A' or '0' or '?' – is always treated as a character, unless we explicitly tell Java to treat it as an integer. So if we display a literal's value

System.out.printIn('a');

the letter 'a' will be displayed. Similarly, if we assign 'a' to a
char variable and then display the variable's value,

char ch = 'a';
System.out.printIn(ch);     // Displays 'a'

the letter 'a' will be shown. If, on the other hand, we wish to output a character's integer value, we must use an explicit cast operator as follows:

System.out.printIn((int)'a');     // Displays 97

A cast operation, such as (int), converts one type of data ('a') into another (97). This is known as a type conversion. Similarly, if we wish to store a character's integer value in a variable, we can cast the char into an  int as follows:

int k = (int)'a';           //  Converts 'a' to 97

System.out.printIn(k);  // Displays 97


As these examples show, a cast is a type conversion operator. Java allows a wide variety of both explicit and implicit type conversions. Certain conversions (for example, promotions) take place when methods are invoked, when assignment statements are executed, when expressions are evaluated, and so on.

Type conversion in Java is governed by several rules and exceptions. In some cases Java allows the programmer to make implicit cast conversions. For example, in the following assignment a char is converted to an int even though no explicit cast operator is used:


char ch;
int k;
k = ch;    //   convert a char into an int

Java permits this conversion because no information will be lost. A character char is represented in 16 bits whereas an  int is represented in 32 bits. This is like trying to put a small object into a large box. Space will be left over, but the object will fit inside without being damaged. Similarly, storing a 16-bit char in a 32-bit  int will leave the extra 16 bits unused. This widening primitive conversion  changes one primitive type (char) into a wider one (int ), where a type's  width is the number of bits used in its representation.

On the other hand, trying to assign an int value to a char variable leads to a syntax error:

char ch;
int k;
ch = k;          // Syntax error:  can't assign int to char

Trying to assign a 32-bit int to 16-bit char is like trying to fit a big object into an undersized box. The object won't fit unless we shrink it in some way. Java will allow us to assign an int value to a char variable, but only if we perform an explicit cast on it:

ch = (char)k;    //   Explicit cast of int k into char ch

The (char) cast operation performs a careful "shrinking" of the int by lopping off the last 16 bits of the int. This can be done without loss of information provided that k's value is in the range 0 to 65535 – that is, in Narrowing conversion the range of values that fit into a  char variable. This narrowing primitive conversion changes a wider type (32-bit int) to a narrower type (16-bit char). Because of the potential here for information loss, it is up to the programmer to determine that the cast can be performed safely.

JAVA LANGUAGE RULE
Type Conversion. Java permits implicit type conversions from a narrower type to a wider type. A cast operator must be used when converting a wider type into a narrower type.

The cast operator can be used with any primitive type. It applies to the variable or expression that immediately follows it. Thus, parentheses must be used to cast the expression m+n into a char:

char ch = (char)(m + n);

The following statement would cause a syntax error because the cast operator would only be applied to m:

char ch = (char)m + n; // Error: right side is an int

In the expression on the right-hand side, the character produced by (char)m will be promoted to an int because it is part of an integer operation whose result will still be an int. Therefore, it cannot be assigned to a char without an explicit cast.