Une variable de type
char
peut contenir des caractères.
La valeur de la variable est représentée par 16 bits.
Une variable de type
char
peut aussi contenir des nombres entiers positifs.
Une variable de type
char
peut contenir les valeurs suivantes :
[0, 65535]
Un
cast
est nécessaire pour affecter une valeur entière a une variable de type
char
.
A character can be expressed as a hexadecimal value using the suffix
\u
Escape Characters:
Unicode escape characters are interpreted before the code get parsed.
-
For example "\u0022 + \u0022" is equivalent to "" + ""
-
\u0061 is the unicode value of the character 'a'
Some cases may cause the compiler to complain about a syntax error:
-
The following will be interpreted as if you have typed
System.out.println("" "");
:
-
Line feed within a comment:
-
Invalid unicode character prefix within a comment:
Please note that the type
char
can hold any 16 bit (basic) unicode character.
For the supplementary unicode characters, each unicode character has a code value (code point) which is represented as pairs of 16 bit code units.
When working with strings that might hold supplementary unicode characters,
you should be aware that a
char
value might actually hold one the code units that represent the supplementary unicode character.
For example, the smiley symbol "🙂" is represented with 2 code units:
If you need to print all characters of a string, use the
codePoints
method of the
String
class:
See the Java docs API for the usage of
Character.toChars
method: