Char data type in Java
In Java, the char data type is used to represent a single 16-bit Unicode character. It can store any Unicode character, from letters and numbers to special symbols and emoticons.
Using char
To use the char type, you need to use the following syntax:
<code class="java">char variableName = 'value';</code>
Where:
variableName
is the name of the char variable to be created. value
is the Unicode character to be stored in the variable, enclosed in single quotes. Example
The following example creates a char variable named myChar
and sets its value to the letter "A":
<code class="java">char myChar = 'A';</code>
Usage
The char type can be used for a variety of purposes, including:
Special Characters
Java provides special character sequences to represent some common characters as follows:
Special sequence | Characters |
---|---|
'\n' | Line break |
'\t' | Tab character |
'\b' | Backspace character |
'\r' | Carriage return character |
'\f' | Form feed character |
'\"' | Double quotes |
'\'' | Single quotes |
'\' | Backslash |
Type conversion
char type can be used with other Data types (such as int, byte and float) are converted as follows:
The above is the detailed content of How to use char in java. For more information, please follow other related articles on the PHP Chinese website!