Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A Char in Kotlin represents a single 16-bit Unicode character. On the JVM, it is backed by the primitive char type and represents a UTF-16 code unit. Unlike Java, Kotlin enforces strict type safety and does not treat characters as numeric types; implicit widening conversions from Char to Int or other numeric types are strictly prohibited.

Instantiation and Syntax

Character literals are defined by enclosing the character in single quotes. They can be instantiated using standard characters, Unicode escape sequences, or explicit conversion from integer code points.
val letter: Char = 'A'
val unicodeChar: Char = '\u0041' // Represents 'A'
val fromCode: Char = Char(65)    // Represents 'A'

Escape Sequences

Kotlin supports standard escape sequences for special characters, prefixed with a backslash (\):
  • \t – Tab
  • \b – Backspace
  • \n – Newline (LF)
  • \r – Carriage return (CR)
  • \' – Single quote
  • \" – Double quote
  • \\ – Backslash
  • \$ – Dollar sign (required to escape string interpolation syntax)

Operations

Char supports specific arithmetic and relational operations evaluated against their underlying Unicode code points. Arithmetic Operations Adding or subtracting an Int to or from a Char yields a new Char offset by that integer value. Subtracting one Char from another yields an Int representing the mathematical distance between their code points.
val nextChar: Char = 'A' + 1  // 'B'
val prevChar: Char = 'C' - 1  // 'B'
val distance: Int = 'Z' - 'A' // 25
Comparison and Ranges Char implements the Comparable<Char> interface, enabling the use of standard relational operators (<, <=, >, >=). It also supports range operators (.., ..<) to generate a CharRange progression.
val isGreater: Boolean = 'b' > 'a' // true
val charRange: CharRange = 'a'..'z'
val halfOpenRange: CharRange = 'A'..<'Z'

Type Conversion

Because Char is not implicitly treated as a number, explicit property access is required to retrieve its underlying integer value. As of Kotlin 1.5, the .code property is the standard mechanism for this extraction, replacing the deprecated .toInt() function.
val char: Char = 'X'
val codePoint: Int = char.code // 88

// Converting an Int back to a Char
val restoredChar: Char = Char(codePoint) // 'X'

Nullability and Boxing

When a Char is declared as nullable (Char?), used as a generic type argument (e.g., List<Char>), or instantiated as a generic array (Array<Char>), the Kotlin compiler automatically boxes the primitive into a java.lang.Character object on the JVM. This boxing incurs memory overhead and requires unboxing during arithmetic or relational operations. To avoid boxing overhead when working with arrays of characters, Kotlin provides the specialized CharArray type, which compiles directly to a primitive char[] on the JVM. Non-nullable Char declarations are compiled to unboxed primitives whenever possible.
Master Kotlin with Deep Grasping Methodology!Learn More