Lately I have used arrays a lot. I’ve started finding various quirks and asking many questions around them. One of the weirdest issues happens when I try to initialize a primitive array LongArray with minimum value. Instead of having a values of -922…08L it overflows and becomes 0. Here’s an example:
val longArray = longArrayOf(Long.MIN_VALUE, 0, Long.MAX_VALUE)
println("longArray min: ${longArray[0]}")
println("long min value: ${Long.MIN_VALUE}")
What I see is this:
longArray min: 0
long min value: -9223372036854775808
What just happened? Shouldn’t they be the same value of -922…something?
From definition of Long.MIN_VALUE
I can see this:
Why do we have that -1? Why first println
shows 0 (zero)? I’m confused.