Bird
Raised Fist0
Kotlinprogramming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following correctly declares a Char variable in Kotlin?
easy
A. val letter: Char = `A`
B. val letter: Char = "A"
C. val letter: Char = A
D. val letter: Char = 'A'

Solution

  1. Step 1: Understand Char declaration syntax

    In Kotlin, a Char value must be enclosed in single quotes, like 'A'.
  2. Step 2: Check each option

    val letter: Char = 'A' uses single quotes correctly. val letter: Char = "A" uses double quotes (String). val letter: Char = A has no quotes (invalid). val letter: Char = `A` uses backticks (invalid for Char).
  3. Final Answer:

    val letter: Char = 'A' -> Option D
  4. Quick Check:

    Char uses single quotes = A [OK]
Hint: Char values use single quotes, not double quotes [OK]
Common Mistakes:
  • Using double quotes instead of single quotes
  • Omitting quotes around the character
  • Using backticks instead of quotes
2. Which of the following is the correct way to get the Unicode code of a Char in Kotlin?
easy
A. val code = 'A'.code
B. val code = 'A'.unicodeValue()
C. val code = 'A'.toInt()
D. val code = 'A'.unicode

Solution

  1. Step 1: Recall Kotlin Char Unicode property

    Kotlin provides the code property to get the Unicode integer value of a Char.
  2. Step 2: Evaluate options

    val code = 'A'.code uses code property correctly. val code = 'A'.toInt() uses toInt() which does not exist for Char. Options B and D use non-existent methods/properties.
  3. Final Answer:

    val code = 'A'.code -> Option A
  4. Quick Check:

    Use .code to get Unicode number [OK]
Hint: Use .code property to get Unicode number from Char [OK]
Common Mistakes:
  • Using non-existent toInt() method on Char
  • Trying non-existent methods like unicodeValue()
  • Accessing properties that don't exist
3. What is the output of the following Kotlin code?
val ch: Char = 'Z'
println(ch.code)
medium
A. 122
B. 90
C. Z
D. SyntaxError

Solution

  1. Step 1: Identify the Char and property used

    The variable ch is assigned the character 'Z'. The code prints ch.code, which returns the Unicode integer value.
  2. Step 2: Find Unicode value of 'Z'

    The Unicode code for uppercase 'Z' is 90.
  3. Final Answer:

    90 -> Option B
  4. Quick Check:

    'Z'.code = 90 [OK]
Hint: Remember uppercase 'Z' Unicode is 90 [OK]
Common Mistakes:
  • Confusing uppercase and lowercase Unicode values
  • Expecting the character itself instead of code
  • Thinking code property causes syntax error
4. Identify the error in this Kotlin code snippet:
val ch: Char = "A"
println(ch.code)
medium
A. code property does not exist for Char
B. Missing semicolon after println
C. Using double quotes for Char assignment
D. Variable ch should be Int, not Char

Solution

  1. Step 1: Check Char assignment syntax

    Chars must be assigned using single quotes, not double quotes. Double quotes create a String.
  2. Step 2: Verify other parts

    Semicolons are optional in Kotlin, so no error there. The code property exists for Char. Variable type Char is correct.
  3. Final Answer:

    Using double quotes for Char assignment -> Option C
  4. Quick Check:

    Char needs single quotes, not double [OK]
Hint: Chars use single quotes; double quotes mean String [OK]
Common Mistakes:
  • Thinking semicolons are mandatory
  • Believing code property is invalid
  • Confusing Char and Int types
5. Given a list of characters val chars = listOf('a', 'B', 'c', 'D'), which Kotlin expression correctly filters only uppercase letters using Unicode properties?
hard
A. chars.filter { it.isUpperCase() }
B. chars.filter { it.code > 90 }
C. chars.filter { it.code.isUpperCase() }
D. chars.filter { it > 'Z' }

Solution

  1. Step 1: Understand filtering uppercase letters

    Kotlin's Char class has a method isUpperCase() that returns true if the character is uppercase.
  2. Step 2: Evaluate each option

    chars.filter { it.isUpperCase() } uses isUpperCase() correctly. chars.filter { it.code > 90 } filters by code > 90, which excludes all uppercase letters (A-Z: 65-90). chars.filter { it.code.isUpperCase() } calls isUpperCase() on an Int (code), which does not exist. chars.filter { it > 'Z' } compares chars incorrectly.
  3. Final Answer:

    chars.filter { it.isUpperCase() } -> Option A
  4. Quick Check:

    Use isUpperCase() to filter uppercase chars [OK]
Hint: Use isUpperCase() method to filter uppercase chars [OK]
Common Mistakes:
  • Filtering by numeric code incorrectly
  • Calling isUpperCase() on the Unicode code (Int)
  • Using wrong comparison operators for chars
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard