skip to content

In Kotlin, is Char a numeric type? How do you get the numeric (code-point) value of a Char, and how do you turn a digit Char like '7' into the Int 7?

level: juniorimportance: must knowfreq 70%

answer

  1. Char is not a Number
  2. .code for the integer code point
  3. .digitToInt() turns '7' into 7
  4. '7'.code == 55, not 7
  5. Char(n) / n.toChar() to build a Char

basics

~10 s

No. A Char in Kotlin is a character, not a number. To get its underlying code you use .code, and to turn a digit character like '7' into the number 7 you use .digitToInt().

solid answer

~40 s

Kotlin's Char is a distinct type, not an Int, and there is no implicit conversion between them (unlike Java where a char auto-promotes to int in arithmetic). To get the UTF-16 code unit you call ch.code (returns Int); to build a Char from a code you use Char(intValue) or intValue.toChar(). For digit characters, '7'.digitToInt() returns 7 (and '7'.digitToIntOrNull() returns 7 or null for non-digits), while 7.digitToChar() goes the other way. Comparisons and ranges work directly ('a'..'z'), and 'a' + 1 yields a Char ('b') because Char has plus(Int): Char operators. Doing '7' - '0' also works (Char.minus(Char) returns Int) but .digitToInt() is the idiomatic, readable choice.

code

kotlin · 6 lines
kotlin
val c = '7'
println(c.code)          // 55  (UTF-16 code unit)
println(c.digitToInt())  // 7   (numeric value)
println('a' + 1)         // b   (Char + Int -> Char)
println('9' - '0')       // 9   (Char - Char -> Int)
println(Char(65))        // A   (build Char from code)

go deeper

for a junior

Knows Char isn't a number and that .code and .digitToInt() exist and differ.

for a middle

Explains the deprecation of toInt(), the Char(Int) constructor, and digitToIntOrNull/radix overloads.

for a senior

Discusses Char operators (plus/minus), '9' - '0' vs digitToInt() readability, and UTF-16 code-unit semantics.

for a principal

Frames the no-implicit-widening design as deliberate type-safety, and notes surrogate-pair caveats: a Char is a UTF-16 unit, not a full Unicode code point.

## Char is its own type In Kotlin, `Char` represents a single 16-bit Unicode (UTF-16) code unit. Crucially, **`Char` is NOT a subtype of `Int` and is NOT a `Number`**. Unlike Java — where `char` silently promotes to `int` in arithmetic so `'a' + 1` is the int `98` — Kotlin keeps them separate. This prevents a whole class of accidental-numeric bugs. ## Getting the numeric value: `.code` To read the integer code point of a character, use the `code` property: ```kotlin val c: Char = 'A' val n: Int = c.code // 65 val back: Char = Char(66) // 'B' (constructor from Int) val back2: Char = 65.toChar() // 'A' ``` Historically Kotlin had `Char.toInt()` / `Int.toChar()`, but `Char.toInt()` is **deprecated** — prefer `.code` and the `Char(Int)` constructor for clarity. ## Digit characters: `.digitToInt()` The character `'7'` has code point 55, NOT 7. To get the *numeric value* of a digit character, use: ```kotlin '7'.digitToInt() // 7 'F'.digitToInt(16) // 15 (radix overload) 'x'.digitToIntOrNull() // null (not a digit) 7.digitToChar() // '7' ``` `digitToInt()` throws `IllegalArgumentException` if the char is not a digit in the given radix; the `...OrNull` variant returns `null` instead. ## Char arithmetic that IS allowed Char defines a small set of operators: - `Char.plus(Int): Char` and `Char.minus(Int): Char` → shift by code: `'a' + 1 == 'b'`. - `Char.minus(Char): Int` → distance between codes: `'9' - '0' == 9`. So `'9' - '0'` is a valid alternative to `.digitToInt()`, but the named function is more readable and locale-safe. ## Ranges and comparison Char is `Comparable<Char>` and supports ranges: ```kotlin for (ch in 'a'..'z') print(ch) val isLetter = c in 'A'..'Z' ``` Useful helpers: `c.isDigit()`, `c.isLetter()`, `c.isWhitespace()`, `c.lowercaseChar()`, `c.uppercaseChar()`. ## Key takeaways - `Char` ≠ `Int`; no implicit widening. - Code value → `.code`; build from code → `Char(n)` / `n.toChar()`. - Digit value → `.digitToInt()` / `.digitToIntOrNull()`; reverse → `Int.digitToChar()`.

  • What is the difference between '7'.code and '7'.digitToInt()?
    '7'.code is the UTF-16 code point (55); '7'.digitToInt() is the numeric value of the digit (7). They differ by the code of '0' (48).
  • Why is Char.toInt() discouraged now?
    It was ambiguous and is deprecated; use .code for the code point. This makes the intent explicit and avoids confusion with digit value.

A Char is like a labeled key on a keyboard — it has a position number (.code) but the label '7' isn't the same as the quantity seven.

saying these in an interview costs you the question

  • Claiming '7'.code returns 7
  • Saying Char is a subtype of Int / a Number
  • Assuming Java-style implicit char-to-int promotion in arithmetic
  • Using toInt()/toChar() and being unaware .code is preferred
  • Thinking digitToInt() works on any letter (it throws unless it's a valid digit)

context