Java examples for Language Basics:char
The char data type is a 16-bit unsigned Java primitive data type.
It represents a Unicode character.
char is an unsigned data type.
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 can be expressed by enclosing it in single quotes.
char c1 = 'A'; char c2 = 'L'; char c3 = '1'; char c4 = '/';
A String literal cannot be assigned to a char variable.
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 enclosed in single quotes.
There are eight predefined character escape sequences as listed in the following table.
Character Escape Sequence | Description |
---|---|
'\n' | A linefeed |
'\r' | A carriage return |
'\f' | A form feed |
'\b' | A backspace |
'\t' | A tab |
'\\' | A backslash |
'\"' | A double quote |
'\'' | A single quote |
char c1 = '\n'; // Assigns a linefeed to c1 char c2 = '\"'; // Assigns double quote to c2 char c3 = '\a'; // A compile-time error. Invalid character escape sequence
A character literal can also be expressed as a Unicode escape sequence in the form '\uxxxx'.
xxxx represents exactly four hexadecimal digits.
The value represented by xxxx is the Unicode value for the character.
The character 'A' has the Unicode value of 65.
The value 65 in decimal can be represented in hexadecimal as 41.
So, the character 'A' can be expressed in Unicode escape sequence as '\u0041'.
char c1 = 'A'; char c2 = '\u0041'; // Same as c2 = 'A'
A character literal can also be expressed as an octal escape sequence in the form '\nnn'.
Here, n is an octal digit (0-7). The range for the octal escape sequence is '\000' to '\377'.
The octal number 377 is the same as the decimal number 255.
Therefore, using octal escape sequence, you can represent characters whose Unicode code range from 0 to 255 decimal integers.
A Unicode character set (code range 0 to 65535) can be represented as a Unicode escape sequence ( '\uxxxx').
char c1 = '\52'; char c2 = '\141'; char c3 = '\400'; // A compile-time error. Octal 400 is out of range char c4 = '\42'; char c5 = '\10'; // Same as '\n'
You can also assign an int literal to a char variable if int literal falls in the range 0-65535.
char c1 = 97; // Same as c1 = 'a'; c1 = '\141'; // or, c1 = '\u0061';
You cannot assign a value stored in a byte variable to a char variable because byte is a signed data type whereas char is an unsigned data type.
In such a case, you need to use an explicit cast.