In Java how can two different primitive data types such as int a = 3
and byte b = 3
be compared?
I note the size of int
is 4 bytes while byte
is only 1 byte. Is it a bitwise comparison?
It doesn't. It never will.
int a = 3;
byte b = 3;
if (a == b) {
...
}
This is not a comparison between an int
and a byte
. ==
can only compare primitives of the same type. So this is a comparison between an int
and an int
(since int
is the wider of the two). It is equivalent to the following more explicit code.
int a = 3;
byte b = 3;
if (a == (int)b) {
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With