bhavneet kaur wrote: but how then b1 + 1 is a int , i have declared bi also byte???
Yes you declared b1 and b2 both as
byte. The range of
byte data type is -127 to 128. So when you are writing
i.e. you are storing 5 in b1. Here there won't be any error, since 5 is in the range of -127 to 128
When you wrote
Here the
Java compiler has no idea about what is the answer of "b1 + 1" which you are storing in
byte variable b2.
The answer of "b1 + 1" can be 50 or 20 or 129 or 300 etc. depending on the value of b1. Fine.
So compiler thinks what if the answer of "b1 + 1" is greater than 128 (i.e. the range allowed in byte variable.).
Therefore compiler gives you an error, saying that variable "b2" in which you are storing "b1 +1" has to
int or you explicitly do the type casting i.e.