What if your app could understand every letter and emoji in the world without breaking?
Why Char type and Unicode behavior in Kotlin? - Purpose & Use Cases
Imagine you want to handle letters and symbols from many languages in your app, but you try to treat each character as a simple number or byte.
This manual way breaks easily because many characters need more than one number to represent them, causing wrong letters, crashes, or lost data.
The Char type with Unicode support lets you work with all characters correctly, no matter the language or symbol, making your app reliable and global.
val letter = 65 // ASCII code for 'A' println(letter.toChar())
val letter: Char = 'A'
println(letter)You can now handle text from any language or emoji smoothly, making your programs truly universal.
Displaying user names with emojis or characters from Chinese, Arabic, or Hindi without errors or weird symbols.
Manual number-based character handling is error-prone.
Char type uses Unicode to represent all characters correctly.
This makes apps support global languages and symbols easily.
Practice
Char variable in Kotlin?Solution
Step 1: Understand Char declaration syntax
In Kotlin, a Char value must be enclosed in single quotes, like 'A'.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).Final Answer:
val letter: Char = 'A' -> Option DQuick Check:
Char uses single quotes = A [OK]
- Using double quotes instead of single quotes
- Omitting quotes around the character
- Using backticks instead of quotes
Char in Kotlin?Solution
Step 1: Recall Kotlin Char Unicode property
Kotlin provides thecodeproperty to get the Unicode integer value of a Char.Step 2: Evaluate options
val code = 'A'.code usescodeproperty correctly. val code = 'A'.toInt() usestoInt()which does not exist for Char. Options B and D use non-existent methods/properties.Final Answer:
val code = 'A'.code -> Option AQuick Check:
Use .code to get Unicode number [OK]
- Using non-existent toInt() method on Char
- Trying non-existent methods like unicodeValue()
- Accessing properties that don't exist
val ch: Char = 'Z' println(ch.code)
Solution
Step 1: Identify the Char and property used
The variablechis assigned the character 'Z'. The code printsch.code, which returns the Unicode integer value.Step 2: Find Unicode value of 'Z'
The Unicode code for uppercase 'Z' is 90.Final Answer:
90 -> Option BQuick Check:
'Z'.code = 90 [OK]
- Confusing uppercase and lowercase Unicode values
- Expecting the character itself instead of code
- Thinking code property causes syntax error
val ch: Char = "A" println(ch.code)
Solution
Step 1: Check Char assignment syntax
Chars must be assigned using single quotes, not double quotes. Double quotes create a String.Step 2: Verify other parts
Semicolons are optional in Kotlin, so no error there. Thecodeproperty exists for Char. Variable type Char is correct.Final Answer:
Using double quotes for Char assignment -> Option CQuick Check:
Char needs single quotes, not double [OK]
- Thinking semicolons are mandatory
- Believing code property is invalid
- Confusing Char and Int types
val chars = listOf('a', 'B', 'c', 'D'), which Kotlin expression correctly filters only uppercase letters using Unicode properties?Solution
Step 1: Understand filtering uppercase letters
Kotlin's Char class has a methodisUpperCase()that returns true if the character is uppercase.Step 2: Evaluate each option
chars.filter { it.isUpperCase() } usesisUpperCase()correctly. chars.filter { it.code > 90 } filters by code > 90, which excludes all uppercase letters (A-Z: 65-90). chars.filter { it.code.isUpperCase() } callsisUpperCase()on an Int (code), which does not exist. chars.filter { it > 'Z' } compares chars incorrectly.Final Answer:
chars.filter { it.isUpperCase() } -> Option AQuick Check:
Use isUpperCase() to filter uppercase chars [OK]
- Filtering by numeric code incorrectly
- Calling isUpperCase() on the Unicode code (Int)
- Using wrong comparison operators for chars
Ready to prove it?
Pass the test and this pattern moves unknown
