byte b=5;
Integer i=(int)b;//b cast to int and int wrapped into Integer
Integer k=(byte)b;//compilation error, cannot convert from byte to Integer
Integer z=(byte)5;//compiles
我的问题是为什么 Integer z=(byte)5
可以编译而 Integer k=(byte)b
不能?就此而言,Integer z1 = (byte)5L
和 Integer z2 = (byte)5.3F
也可以编译。是不是因为我试图转换一个编译时间常量而转换对它没有影响?
最佳答案
作为作业
整数 z=(byte)5
使用文字值,编译器会立即将其翻译成
Integer z = Integer.valueOf(5);
编译器不够智能,无法像这种情况那样引用变量:
Integer k= (byte)b;
关于java - 类型转换和编译时间常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16344903/