Kotlin Char()

Kotlin Char() Function

The Kotlin Char() function is used to create a Char instance with the specified code value.

In this tutorial, you shall learn the syntax of Kotlin Char() function, and how to use it with examples.

Syntax

The syntax of Char() function is

fun Char(code: Int): Char

where

ParameterDescription
codeThe character code to be used in creating the Char instance.

Return value

The Char() function returns a Char instance.

Example 1: Creating a Char Instance

In this example, we’ll use the Char() function to create a Char instance with the character code 68.

Kotlin Program

fun main() {
    val charInstance = Char(68)
    println("Character: $charInstance")
}

Output

Character: D

The output shows the Char instance created with the code 68 using the Char() function.

Example 2: Creating a Char Instance from Unicode

Let’s use the Char() function to create a Char instance from its Unicode value.

This example demonstrates creating a Char instance with the character ‘❤’ using its Unicode value.

Kotlin Program

fun main() {
    val charInstance = Char(0x2764)
    println("Character: $charInstance")
}

Output

Character: ❤

The output shows the Char instance created with the Unicode value 0x2764, representing the heart symbol ‘❤’.

Summary

In this tutorial, we have seen the syntax of Kotlin Char() function, and how to use it to create a Char instance, with examples.