Char Data type Usage and Type Conversion in Kotlin

Definition

  • Char data type is represented by single apostrophe just like Java in Kotlin.
  • In JVM, the characteristics of this "Char" variable is derived from the characteristics of primitive type "char" of Java which has a non-nullable default value.

Syntax and declaration

A Char value can be declared by 3 ways as mentioned below.

1. Mentioning type explicitly

val variable_name : Char = 'v'

for example,

val name : Char = 'r'

2. Type is automatically inferred

val variable_name = 'v'

for example,

val name = 'r'

3. Declaring and Initializing separately

var variable_name : Char
variable_name = ''

for example

//Declaring and Initializing separately
var name:Char
name = 'r'

Char Data type Example Program in Kotlin

// Char Datatype Kotlin example program
// Data type Kotlin Programs, Basic Kotlin Programs

fun main(args:Array<String>) {
    //Assigned value is inferred automatically by compiler
    val name = 'm'
    println("The character is: $c")
}

Sample Output

The character is: m