# Characters The [Char](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char/) type represents a single character as a UTF-16 code unit. Use `Char` for individual character values, such as letters, digits, punctuation marks, or whitespace. For sequences of characters, use [String](strings.html). Tip: `Char` is not a numeric type, but each character has a numeric Unicode value that you can access. See [Character conversion](#character-conversion). ## Syntax To declare a character, enclose the value in single quotes (`' '`). You can specify the `Char` type explicitly or let Kotlin infer it from the value: ```KOTLIN val letter: Char = 'a' // Kotlin infers Char because the values are written in single quotes val digit = '1' val symbol = '!' val space = ' ' val separator = ':' ``` A character literal must contain exactly one character. Otherwise, the Kotlin compiler reports an error: ```KOTLIN val invalid = 'AB' // Error val invalidEmpty = '' // Error ``` ### Nullable values To store a nullable value, use `Char?`: ```KOTLIN val maybeAbsent: Char? = null ``` Note: On the JVM, nullable `Char` values are boxed when needed. The same applies to [numeric types](numbers.html#boxing-and-caching-numbers-on-the-jvm). ## Unicode support Kotlin represents `Char` values as UTF-16 code units. This means that a single `Char` stores one UTF-16 code unit, not necessarily one complete Unicode character. ### Basic Multilingual Plane A single `Char` can store values in the range from `\u0000` to `\uFFFF`. This range covers the Basic Multilingual Plane (BMP) that includes characters for almost all modern languages and a large number of symbols. To specify a character by the Unicode value, use `\u` followed by four-digit hexadecimal value from the [Unicode table](https://www.unicode.org/charts/): ```KOTLIN val unicodeNumber = '\u0031' // Equals '1' ``` ### Supplementary characters Unicode characters outside the BMP, such as emojis and some historic scripts, cannot be represented by a single `Char`. In UTF-16, they are encoded as a surrogate pair, where two `Char` values together represent one Unicode character in a `String`: ```KOTLIN fun main() { //sampleStart val emoji = "🥦" println(emoji.length) // 2 println(emoji[0]) // First surrogate println(emoji[1]) // Second surrogate //sampleEnd } ``` Tip: To handle 32-bit symbols individually, use Unicode code points stored as `Int` values. ## Escape sequences Use escape sequences for special characters that are difficult to write directly in source code or have a special meaning. Every escape sequence begins with a backslash (`\`). | Supported sequence |Description | ----------------------------------- | `\t` |Tab | | `\b` |Backspace | | `\n` |New Line (LF) | | `\r` |Carriage Return (CR) | | `\'` |Single quotation mark | | `\"` |Double quotation mark | | `\\` |Backslash | | `\$` |Dollar sign | For example: ```KOTLIN val newLine = '\n' val dollar = '\$' val backslash = '\\' ``` ## Operations `Char` supports comparison, inspection, case conversion, and explicit numeric conversion. ### Character comparison To compare `Char` values, use standard [comparison operators](keyword-reference.html#operators-and-special-symbols) such as `==`, `!=`, `<`, `>`, `<=`, and `>=`. Kotlin compares `Char` values by their numeric Unicode values and returns a `Boolean` value: ```KOTLIN val before = 'a' < 'b' // true val after = 'c' > 'd' // false val different = 'A' == 'a' // false val equal = 'A' == 'A' // true ``` ### Character processing Kotlin provides functions for inspection and case conversion of character values. For example: ```KOTLIN fun main() { //sampleStart val myChar = 'A' // Checks if the character represents a digit println(myChar.isDigit()) // false // Checks if the character represents an uppercase letter println(myChar.isUpperCase()) // true // Returns a lowercase version println(myChar.lowercaseChar()) // 'a' //sampleEnd } ``` Note: Learn more about available functions in the [API Reference](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-char/). ### Character arithmetic You can create another character value by adding or subtracting an integer: ```KOTLIN fun main() { //sampleStart val a = 'a' println(a + 1) // b println(a + 2) // c println(a - 32) // A //sampleEnd } ``` Note: These operations follow Unicode values, not language-specific alphabet rules. You can also use the increment (`++`) and decrement (`--`) operators in the prefix and postfix forms with mutable variables: ```KOTLIN fun main() { //sampleStart var a = 'A' a += 10 println(a) // 'K' println(++a) // 'L' prefix increment println(a++) // 'L' postfix increment println(a) // 'M' println(--a) // 'L' prefix decrement println(a--) // 'L' postfix decrement println(a) // 'K' //sampleEnd } ``` ### Character conversion To convert `Char` to a numeric type, use explicit conversion: * Use `.code` to get the numeric Unicode value of a character: ```KOTLIN fun main() { //sampleStart val letter = 'A' println(letter.code) // 65 //sampleEnd } ``` * If a character represents a decimal digit, use `digitToInt()`: ```KOTLIN fun main() { //sampleStart val digit = '7' println(digit.digitToInt()) // 7 //sampleEnd } ``` Tip: If the character may not be a valid digit, use `digitToIntOrNull()`.