Characters are represented by the type Char
. Character literals go in single quotes: '1'
.
Special characters start from an escaping backslash \
.
The following escape sequences are supported:
\t
– tab\b
– backspace\n
– new line (LF)\r
– carriage return (CR)\'
– single quotation mark\"
– double quotation mark\\
– backslash\$
– dollar sign
To encode any other character, use the Unicode escape sequence syntax: '\uFF00'
.
fun main() {
//sampleStart
val aChar: Char = 'a'
println(aChar)
println('\n') // Prints an extra newline character
println('\uFF00')
//sampleEnd
}
{kotlin-runnable="true" kotlin-min-compiler-version="1.3"}
If a value of character variable is a digit, you can explicitly convert it to an Int
number using the digitToInt()
function.
On JVM: Like numbers, characters are boxed when a nullable reference is needed. Identity is not preserved by the boxing operation.
{type="note"}