0

//code start

int a=3;
byte b=3;
if(a==b)  --> returns true

//code ends

  • Query :

    IT returns true because compares the bits in a and b. a = 00000011 b = 00000011

    here only bit pattern comparison is done and zeros left at the end in int doesn't matter.

    what does the above line conveys??

Siva
  • 17
  • 7

2 Answers2

1

In java automatic type conversion converts any small data type to the larger of the two types. So byte b is converted to int b when you are comparing it with int a.

Do know that double is the largest data type while byte is the smallest.

enter image description here

ArbitraryChoices
  • 243
  • 1
  • 11
1

Rather than only comparing the least significant 8 bits of the two operands like you guessed, the compiler adds 24 0s or (or 1s if the byte is negative) to the byte to make it the same size as the int. This is known as binary numeric promotion and is specified in §5.6.2 of the JLS. The conversion from byte to int is specified in §5.1.2.

Sweeper
  • 213,210
  • 22
  • 193
  • 313