I’d a code snippet:
class AutoTypeCast{
public static void main(String...args){
int x=10;
byte b=20;//no compilation error
byte c=x;//compilation error
}
}
Why 20 is automatically type-casted to byte while x not?
Because
xis anintand has a wider range asbyte. That is why there may be data loss is you assign it tobyte.20is a constant and while compile time garanteed to be in the range ofbyte.