Search code examples
javabytewrapper

assigning value to the byte


what is the difference between this two

Byte i1=new Byte(1);//complier error
byte b=1;//ok

my question is about assigning the value 1 to byte where 1 is int literal. but when passing 1 to the Byte class constructor it gives error


Solution

  • The value 1 is a literal of type integer. So you have the following situations:

    • Assigning the literal directly to a variable of type byte will cause an implicit conversion since it is obvious that the programmer wants a byte and not an int.

    • The Byte ctor takes a byte value, the compiler complains since it can't do an implicit conversion for method or ctor arguments.