Blog: Character Study - Understanding the Char Type in Kotlin
Day 28! Today I focused on one of the smallest units of data: the Character.
Not Your Average Number
Coming from some languages where characters and integers are basically the same thing, Kotlin’s approach is a breath of fresh air. In Kotlin, a Char is its own thing. You can't just assign 65 to a character variable; you have to be intentional. This small restriction prevents a lot of silly bugs where you might accidentally treat a letter like a number.
The Escape Artists
We all know \n for a new line, but Kotlin’s support for escape sequences is robust. I especially liked seeing \$. Since Kotlin uses the dollar sign for string templates, you need a way to tell the compiler, "No, I actually want a real dollar sign here!"
Unicode and the Emoji Mystery
I did a deep dive into how Kotlin handles Unicode. It was fascinating to see that a single emoji, like 🚀, actually has a length of 2 in a Kotlin string. This is because emojis are "supplementary characters" that require two UTF-16 code units (surrogate pairs) to be represented. It’s a great reminder that "one character" isn't always "one unit of memory."
Smart Conversions
Kotlin makes it incredibly easy to work with digits. Instead of doing the old trick of char - '0' to get an integer, you just call .digitToInt(). It's readable, safe, and handles non-digit characters gracefully if you use the OrNull version.
Summary
Characters in Kotlin are more than just bits and bytes; they are a well-designed part of a type-safe ecosystem. Whether you’re parsing data or just printing emojis, the language gives you the tools to do it safely and efficiently.
Check out the full technical breakdown!
Comments
Post a Comment