Please take a look at below example i cant understand the relation between char and byte
byte b = 1;
char c = 2;
c = b; // line 1
Give me compilation Error because c is type of char and b is type of byte so casting is must in such condition
but now the tweest here is when i run below code
final byte b = 1;
char c = 2;
c = b; // line 2
line 2 compile successfully it doesn't need any casting at all
so my question is why char c behave different when i use final access modifier with byte
Because qualifying it with
finalmakes the variable a constant variable, which is a constant expression. Soactually becomes
And the compiler has a guarantee that the value
1can fit in acharvariable.With a non constant
bytevariable, there is no such guarantee.byteis signed,charis unsigned.