Boolean and Char
- Kotlin course
- Beginner
- Lesson 15/116
- 3 min
Learn Boolean and Char types.
Boolean and Char
Boolean takes only two values: true and false. It is used in conditions (if, while) and logical expressions.
Char represents a single Unicode character and is written between single quotes: 'A', '7', '\n'. Char is not a number; to get a character's code you use the code property.
val active: Boolean = true
val letter: Char = 'K'
println(letter.code) // 75Practice Char code
Write a fun main(). Declare val letter: Char = 'K'. Print the character's code with println(letter.code).
Sample solution
fun main() {
val letter: Char = 'K'
println(letter.code)
}Expected output
75
Char vs String
Char represents one character; String represents zero or more characters. 'A' is a Char; "A" is a String with one character. These are different types and behave differently in arithmetic.
Char + Char produces an Int; String + String concatenates. Mixing them creates compile errors or unexpected behavior.
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How is a Char written?
- "A"
- A
- 'A' Correct answer
- (A)
Explanation: A Char is written between single quotes.
2 Boolean value
val active: Boolean = ____
val active: Boolean = true
Explanation: Boolean values are true or false.
3 Boolean and Char: match the terms
| Term | Meaning |
|---|---|
Boolean |
true/false |
Char |
'A' |
Int |
5 |
String |
"text" |