Kotlin Help

Characters

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.

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:

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:

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

Nullable values

To store a nullable value, use Char?:

val maybeAbsent: Char? = null

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:

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:

fun main() { //sampleStart val emoji = "🥦" println(emoji.length) // 2 println(emoji[0]) // First surrogate println(emoji[1]) // Second surrogate //sampleEnd }

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:

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

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

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:

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 }

Character arithmetic

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

fun main() { //sampleStart val a = 'a' println(a + 1) // b println(a + 2) // c println(a - 32) // A //sampleEnd }

You can also use the increment (++) and decrement (--) operators in the prefix and postfix forms with mutable variables:

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:

    fun main() { //sampleStart val letter = 'A' println(letter.code) // 65 //sampleEnd }

  • If a character represents a decimal digit, use digitToInt():

    fun main() { //sampleStart val digit = '7' println(digit.digitToInt()) // 7 //sampleEnd }

02 April 2026