Blog: Precision Matters - Deep Dive into Kotlin Numbers
Day 25! Today I learned that even something as "basic" as numbers has a lot of thought behind it in Kotlin.
No More Hidden Bugs
In many languages, assigning an int to a long just works. But Kotlin says, "Wait, are you sure?" You have to explicitly call .toLong(). At first, it felt like more typing, but I realized it prevents those annoying bugs where you accidentally mix up data sizes.
Bitwise logic in Plain English
Instead of searching for << or & on my keyboard, Kotlin uses clear names like shl (shift left) and and. It reads like a sentence!
val result = (1 shl 2) and 0x0F
The Mystery of the Box
I discovered that Kotlin is very clever about how it stores numbers. Most of the time, they are efficient primitives. But if you make them nullable (Int?), they get "boxed" into objects. I even saw the JVM "caching" trick where small numbers share the same object to save memory!
NaN is Weird (but predictable)
Did you know that in standard math, NaN is not equal to itself? But if you put it in a Kotlin list or box it, it is equal. It’s these little details that make Kotlin feel like it was built by developers who have seen (and fixed) everything.
Summary
Kotlin Numbers are safe, efficient, and expressive. Whether you’re doing heavy math or just counting loops, the language gives you the precision you need without the hidden surprises.
Check out the full technical breakdown!
Comments
Post a Comment