Java Reference
In-Depth Information
Java has a class called Short (note the upper case S in Short ), which defines two constants to represent maximum
and minimum values of short data type, Short.MAX_VALUE and Short.MIN_VALUE .
short max = Short.MAX_VALUE;
short min = Short.MIN_VALUE;
The char Data Type
The char data type is a 16-bit unsigned Java primitive data type. It represents a Unicode character. Note that char is
an unsigned data type. Therefore, a char variable cannot have a negative value. The range of the char data type is 0 to
65535, which is the same as the range of the Unicode set. A character literal represents a value of the char data type.
A character literal in Java can be expressed in the following formats:
A character enclosed in single quotes
As a character escape sequence
As a Unicode escape sequence
As an octal escape sequence
A character can be expressed by enclosing it in single quotes. The following snippet of code assigns values to char
variables using this form:
char c1 = 'A';
char c2 = 'L';
char c3 = '5';
char c4 = '/';
Recall that a sequence of characters enclosed in double quotes is a String literal. A String literal cannot be
assigned to a char variable, even if the String literal consists of only one character. In fact, the assignment of a String
literal to a char variable is not allowed because of the rule that a value of a reference data type cannot be assigned to
a variable of a primitive data type. All String literals represent objects of String class in Java and hence they are of
reference data type, whereas character literals represent a value of the char primitive data type.
char c1 = 'A'; // OK
String s1 = 'A'; // An error. Cannot assign a char 'A' to a String s1
String s2 = "A"; // OK. "A" is a String assigned to a String variable
String s3 = "ABC"; // OK. "ABC" is a String literal
char c2 = "A"; // An error. Cannot assign a String "A" to char c2
char c4 = 'AB'; // An error. A character literal must contain only one character
A character literal can also be expressed as a character escape sequence. A character escape sequence starts with
a backslash immediately followed by a character, and both are enclosed in single quotes. There are eight predefined
character escape sequences as listed in Table 3-2 .
 
Search WWH ::




Custom Search