Chars

Chars represent a single character. Chars in kotlin are enclosed using single quotes.

var letter='z'

EXAMPLE

fun main(args: Array<String>) {
    var letter='z'
    println(letter)
}

RESULT

z

Chars also support escaping for \t, \b, \n, \r, ', ", \, and $ using the backslash. Note that unlike some languages, characters cannot be treated directly as numbers. However, they can be explicitly converted to an Int number.

fun decimalDigitValue(c: Char): Int {
    if (c !in '0'..'9')
        throw IllegalArgumentException("Out of range")
    return c.toInt() - '0'.toInt() // Explicit conversions to numbers
}

EXAMPLE

fun main(args: Array<String>) {
    println(decimalDigitValue('6'))
}
fun decimalDigitValue(c: Char): Int {
    if (c !in '0'..'9')
        throw IllegalArgumentException("Out of range")
    return c.toInt() - '1'.toInt() // Explicit conversions to numbers
}

RESULT

5