type casting of byte and int

后端 未结 8 1153
-上瘾入骨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 16:55

    X defined as integer, while narrowing there may be data loss, that is why compiler error. Refer jvm spec for conversions & promotions

提交回复
热议问题