0
0
Kotlinprogramming~10 mins

Char type and Unicode behavior in Kotlin - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Char type and Unicode behavior
Declare Char variable
Assign Unicode character
Access Char properties
Print Char and Unicode code
Use Char in expressions
Observe Unicode behavior
This flow shows how a Char variable is declared, assigned a Unicode character, and how its Unicode code can be accessed and used in Kotlin.
Execution Sample
Kotlin
fun main() {
    val ch: Char = '\u0041'
    println(ch)            // Prints character
    println(ch.code)       // Prints Unicode code
}
This code declares a Char with Unicode for 'A', prints the character and its Unicode code.
Execution Table
StepActionVariableValueOutput
1Declare Char variable chchuninitialized
2Assign Unicode '\u0041' to chch'A'
3Print chch'A'A
4Print ch.codech.code6565
5End of main
💡 Program ends after printing character and its Unicode code.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
chuninitialized'A''A''A''A'
ch.codeN/AN/AN/A6565
Key Moments - 2 Insights
Why does printing ch show 'A' but printing ch.code shows 65?
Because ch holds the character 'A', and ch.code gives its Unicode number 65, as shown in execution_table steps 3 and 4.
Can Char hold any Unicode character?
Yes, Char can hold any single Unicode character, including special symbols, by using Unicode escape like '\uXXXX' as in step 2.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what is the value assigned to ch?
A65
B'\u0041'
C'A'
D'a'
💡 Hint
Check the 'Value' column at step 2 in execution_table.
At which step does the program print the Unicode code of ch?
AStep 3
BStep 4
CStep 2
DStep 5
💡 Hint
Look at the 'Output' column in execution_table for the Unicode number.
If ch was assigned '\u03A9' (Greek Omega), what would ch.code print?
A937
B65
C945
DNone
💡 Hint
Unicode '\u03A9' corresponds to decimal 937, check how ch.code prints Unicode numbers.
Concept Snapshot
Char type holds a single Unicode character.
Assign Unicode with '\uXXXX' syntax.
Use ch.code to get Unicode number.
Printing ch shows the character.
Supports all Unicode characters.
Full Transcript
In Kotlin, the Char type stores a single Unicode character. You can assign a character using Unicode escape sequences like '\u0041' for 'A'. When you print the Char variable, it shows the character itself. Using the property ch.code prints the Unicode number of that character, for example 65 for 'A'. This lets you work with characters and their Unicode codes easily. The program declares a Char variable, assigns it a Unicode character, prints the character, then prints its Unicode code, then ends.