kotlin-types-characters.md

#Character conversion

To convert Char to a numeric type, use explicit conversion:

kotlin  val letter = 'A'
  println(letter.code) // 65
kotlin  val digit = '7'
  println(digit.digitToInt()) // 7

#Characters

Last modified: 04 May 2026

The 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.

Char is not a numeric type, but each character has a numeric Unicode value that you can access. See 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:

kotlinval 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:

kotlinval invalid = 'AB' // Error
val invalidEmpty = '' // Error

#Nullable values

To store a nullable value, use Char?:

kotlinval maybeAbsent: Char? = null

On the JVM, nullable Char values are boxed when needed. The same applies to numeric types.


#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:

kotlinval 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:

kotlinfun main() {
    val emoji = "🥦"

    println(emoji.length) // 2
    println(emoji[0])     // First surrogate
    println(emoji[1])     // Second surrogate
}

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 sequenceDescription
\tTab
\bBackspace
\nNew Line (LF)
\rCarriage Return (CR)
\'Single quotation mark
\"Double quotation mark
\\Backslash
\$Dollar sign

Example:

kotlinval 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 such as ==, !=, <, >, <=, and >=.

Kotlin compares Char values by their numeric Unicode values and returns a Boolean value:

kotlinval 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:

kotlinval 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'

#Character arithmetic

You can create another character value by adding or subtracting an integer:

kotlinval a = 'a'

println(a + 1)  // b
println(a + 2)  // c
println(a - 32) // A
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:

kotlinvar 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'

#Character conversion

To convert Char to a numeric type, use explicit conversion:

kotlinval letter = 'A'
println(letter.code) // 65
kotlinval digit = '7'
println(digit.digitToInt()) // 7