[Doubt]Implicit conversion

public class Demo {
public static void main(String[] args) {
byte b = 10;
byte c = 20;
byte d = b+c;
}
}
here byte d is giving compile time error. As after adding it’s in the range of byte then error is coming?

Hi @Amit1506,
See when you add two bytes then the sum operation is calculated at the runtime and hence at compile time the sum is not known to the compiler and the compiler does not know whether the sum that will finally come will be in the range of byte or not … Therefore it gives a compilation error .

1 Like