type casting of byte and int

后端 未结 8 1049
-上瘾入骨i
-上瘾入骨i 2021-01-27 16:27

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;         


        
8条回答
  •  星月不相逢
    2021-01-27 17:15

    Up-casting is automatic where as down-casting or narrowing (byte c=x;) should be explicit as it might cause loss of precision which a programmer should be aware of explicitly. To correct it you need to put an explicit cast byte c=(byte)x;

提交回复
热议问题