https://kotlinlang.org logo
#announcements
Title
# announcements
n

nulldev

03/17/2017, 3:04 AM
Does anybody know why calling
toByte()
on
-128
results in
128
? Java bytes can only hold
-128
to
127
anyways! Example code:
Copy code
fun main(args: Array<String>) {
    println(-128.toByte())
    println(-127.toByte())
}
Result:
Copy code
128
-127
Casting
-128
to
byte
in Java returns
-128
as expected.
u

uli

03/17/2017, 10:13 AM
Interesting enough, factoring out the number to a local val fixes the issue:
Copy code
fun main(args: Array<String>) {
    val v1 = -128
 	println(v1.toByte())
    val v2 = -127
 	println(v2.toByte())
}
7 Views