https://kotlinlang.org logo
#getting-started
Title
# getting-started
r

Robert

10/29/2020, 10:29 PM
Why can’t I do this:
Copy code
val myByte: Byte = 0;
val myInt: Int = 0;
myByte == myInt;
but CAN i actually do this?
Copy code
myByte.compareTo(myInt) == 0;
Isn’t it the same?
v

Vampire

10/29/2020, 10:35 PM
No, Byte and Int are different types. You cannot compare different types with
==
. But
Byte.compareTo
is overloaded with
Byte.compareTo(Int)
, thus it works. You can do
myByte.toInt() == myInt
r

Robert

10/29/2020, 10:42 PM
Okay, too bad it can’t do some sort of smart casting so we can use the simple comparison syntax. But I guess I’ll use the compareTo syntax, seems more flexible in case I change types