ADocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
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.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.
Char implements the Comparable<Char> interface, enabling the use of standard relational operators (<, <=, >, >=). It also supports range operators (.., ..<) to generate a CharRange progression.
Type Conversion
BecauseChar 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.
Nullability and Boxing
When aChar 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





