What is the correct way to compare Byte to Int? `i...
# announcements
r
What is the correct way to compare Byte to Int?
if (myByte.compareTo(myInt) == 0)
?
if (myByte.toInt() == myInt)
?
if (myByte == myInt.toByte())
? Something else? Or can I force implicit conversion somehow, as I have a lot of these comparisons?
j
if (myByte.toInt() == myInt)
would be the most appropriate I think.
r
@jlleitschuh Cool. Is there any reasoning to it? Or is it personal preference?
j
if (myByte == myInt.toByte())
If there is an overflow in the int to byte conversion, you'll have weird behavior.
if (myByte.compareTo(myInt) == 0)
This is just confusing from a readability perspective and I don't even know if it will compile.
h
I definitely wouldn't narrow the `Int`:
if (myByte == myInt.toByte())
.
123456.toByte()
is
64
.
r
Okay, thanks, then I'll go with the
myByte.toInt()