Char

Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
fun Char(code: Int): Char

Creates a Char with the specified code, or throws an exception if the code is out of Char.MIN_VALUE.code..Char.MAX_VALUE.code.

If the program that calls this function is written in a way that only valid code is passed as the argument, using the overload that takes a UShort argument is preferable (Char(intValue.toUShort())). That overload doesn't check validity of the argument, and may improve program performance when the function is called routinely inside a loop.

import java.util.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) //  will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) //  will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF
//sampleEnd
}
Platform and version requirements: JVM (1.5), JS (1.5), Native (1.5)
fun Char(code: UShort): Char

Creates a Char with the specified code.

import java.util.*
import kotlin.test.*

fun main(args: Array<String>) {
//sampleStart
val codes = listOf(48, 65, 122, 946)
println(codes.map { Char(it) }) // [0, A, z, β]
println(codes.map { Char(it.toUShort()) }) // [0, A, z, β]

// Char(-1) //  will fail
println(Char(UShort.MIN_VALUE)) // \u0000
// Char(1_000_000) //  will fail
println(Char(UShort.MAX_VALUE)) // \uFFFF
//sampleEnd
}

© 2010–2021 JetBrains s.r.o. and Kotlin Programming Language contributors
Licensed under the Apache License, Version 2.0.
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-char.html