Char
A Char holds exactly one letter, digit, or symbol, like one tile from a word game. You write it in single quotes.
In this page:
Syntax
val ch: Char = 'a'
Char
A Char represents a single UTF-16 character, written with single quotes, like A. Characters can be compared and cast to their underlying numeric code point with .toInt.
Double quotes are reserved for String, not Char, and mixing them up is a common beginner mistake.
Note:
Casting a Char to Int with
.toInt gives you its Unicode code point.Example: Char
object Main extends App {
val letter: Char = 'A'
val digit: Char = '7'
println(letter)
println(letter.toInt)
println(digit.isDigit)
}
Login to try C/C++/Java/PHP code in the editor
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the
view already swaps in the hi_ translation fields (or blanks these
out if untranslated), so this renders correctly for both languages
without a lang_code check here. #}
Common Mistakes
- Using double quotes for a
Char, as inval c: Char = "A", which is aStringand does not compile. - Expecting
letter.toIntto give the digit's value for7, when it gives the character code55. - Trying to store two characters in a
CharlikeAB, which is a compile error.
🔒
Chapter Quiz — Complete all 8 topics to unlock
0/8 topics done
Complete these topics first: