0
0
Kotlinprogramming~5 mins

Char type and Unicode behavior in Kotlin

Choose your learning style9 modes available
Introduction

The Char type lets you work with single letters or symbols. It uses Unicode, so you can handle many languages and special characters easily.

When you want to store or check a single letter or symbol.
When you need to compare characters, like checking if a letter is uppercase.
When you want to convert characters to their Unicode number or back.
When you process text one character at a time, like reading input.
When you want to handle special symbols or emojis in your program.
Syntax
Kotlin
val letter: Char = 'A'
val symbol: Char = '\u2602' // Unicode for umbrella symbol

Use single quotes ' ' for Char values.

Unicode characters can be written with \u followed by 4 hex digits.

Examples
Stores the letter B as a Char.
Kotlin
val letter: Char = 'B'
Stores the Unicode heart symbol as a Char.
Kotlin
val emoji: Char = '\u2764'
Stores the digit 7 as a Char, not a number.
Kotlin
val digit: Char = '7'
Stores the newline character as a Char.
Kotlin
val newline: Char = '\n'
Sample Program

This program shows how to store characters, print them, check if a letter is uppercase, and get the Unicode number of a character.

Kotlin
fun main() {
    val letter: Char = 'K'
    val unicodeChar: Char = '\u03A9' // Greek capital letter Omega
    println("Letter: $letter")
    println("Unicode char: $unicodeChar")
    println("Is letter uppercase? ${letter.isUpperCase()}")
    println("Unicode code of letter: ${letter.code}")
}
OutputSuccess
Important Notes

The Char type holds a single character, not a string.

You can get the Unicode number of a Char using code property.

Unicode lets you use letters from many languages and symbols like emojis.

Summary

Char stores one character using Unicode.

Use single quotes to write Char values.

You can check character properties and get Unicode numbers easily.