If compiler is able to implicitly convert integer literal into byte type and assign the result to b ( b = 100; ), why can’t it also implicitly assign the result of an expression a+100 ( result is of type integer ) to b?
byte a = 10;
byte b = a; //ok
b = 100; //ok
b = a + 100;//error - explicit cast needed
b = (byte)(a + 100); // ok
thanx
It’s all about static type safety – whether, at compile time, we can safety know the type of an expression. With a literal, the compiler can correctly tell that if it can be converted to a byte. In
byte a = 20, 20 is convertible, so it all goes through fine.byte a = 257won’t work (257 can’t be converted).In the case
byte b = a, then we already know a is a byte, so type safety is assured.b = 100is again fine (it’s statically known that 100 is convertible).In
b = a + 100, it is not statically known ifa + 100is a byte.acould contain 200, soa + 100is not representable as a byte. Hence the compiler forces you to tell it “Yes,a + 100is always a byte” via a cast, by appealing to your higher level programmer knowledge.Some types of more advanced type systems don’t suffer from this problem, but come with their own problems that most programmers won’t like.