Principle 5.9.2. 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.
char
. A character in Java is represented by a 16-bit unsigned integer. This means that a total of \(2^{16}\) or 65536 different Unicode characters can be represented, corresponding to the integer values 0 to 65535.Code 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
Char SP ! " # $ % & ' ( ) * + , - . /
Code 48 49 50 51 52 53 54 55 56 57
Char 0 1 2 3 4 5 6 7 8 9
Code 58 59 60 61 62 63 64
Char : ; < = > ? @
Code 65 66 67 68 69 70 71 72 73 74 75 76 77
Char A B C D E F G H I J K L M
Code 78 79 80 81 82 83 84 85 86 87 88 89 90
Char N O P Q R S T U V W X Y Z
Code 91 92 93 94 95 96
Char [ \ ] ^ _ `
Code 97 98 99 100 101 102 103 104 105 106 107 108 109
Char a b c d e f g h i j k l m
Code 110 111 112 113 114 115 116 117 118 119 120 121 122
Char n o p q r s t u v w x y z
Code 123 124 125 126
Char { | } ~
System.out.println('a');
char
variable and then display the variable’s value,char ch = 'a';
System.out.println(ch); // Displays 'a'
System.out.println((int)'a') ; // Displays 97
(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.println(k); // Displays 97
float
is promoted to a double
) take place automatically when methods are invoked, when assignment statements are executed, when expressions are evaluated, and so on.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
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.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
int
to 16-bit char
is like trying to cram 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
(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 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.char
:char ch = (char)(m + n);
m
:char ch = (char)m + n; // Error: right side is an int
(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.m
and n
are integer variables of type int
and that ch1
and ch2
are character variables of type char
. Determine in each of the cases that follow whether the assignment statements are valid. If not, modify the statement to make it valid. m = n;
m = ch1;
ch2 = n;
ch1 = ch2;
ch1 = m - n;
'0' ... '9'
, and letters, 'a' ... 'z'
and 'A' ... 'Z'
, are represented by sequences of integers (Figure 5.9.1).}
’ because its integer code (91) is less than 125, the integer code for ‘}
’.char
type, the following relational operators can be defined: \(\lt\text{,}\) \(>\text{,}\) \(\lt\)=, \(>\)=, ==, !=. Given any two characters, ch1 and ch2, the expression ch1 \(\lt\) ch2 is true if and only if the integer value of ch1 is less than the integer value of ch2. In this case we say that ch1precedesch2 in lexical order. Similarly, the expression ch1 \(>\) ch2 is true if and only if the integer value of ch1 is greater than the integer value of ch2. In this case we say that ch1followsch2. And so on for the other relational operators. This means that we can perform comparison operations on any two character operands (Table 5.9.3).Operation | Operator | Java | True Expression |
Precedes | \(\lt\) | \(ch1\ \lt \ ch2\) | \('a'\ \lt \ 'b'\) |
Follows | \(>\) | \(ch1\ >\ ch2\) | \('c'\ >\ 'a'\) |
Precedes or equals | \(\lt =\) | \(ch1\ \lt =\ ch2\) | \('a'\ \lt =\ 'a'\) |
Follows or equals | \(>=\) | \(ch2\ >=\ ch1\) | \('a'\ >=\ 'a'\) |
Equal to | \(= =\) | \(ch1\ ==\ ch2\) | \('a'\ ==\ 'a'\) |
Not equal to | \(!\!=\) | \(ch1\ !\!=\ ch2\) | \('a'\ !\!=\ 'b'\) |