Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how java compare two primitive data type with different size

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?

like image 398
Kashyap Avatar asked Jan 03 '23 20:01

Kashyap


1 Answers

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) {
  ...
}
like image 93
Silvio Mayolo Avatar answered Jan 05 '23 11:01

Silvio Mayolo