gpt4 book ai didi

java - 类型转换和编译时间常量

转载 作者:搜寻专家 更新时间:2023-11-01 02:12:18 25 4
gpt4 key购买 nike

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)5LInteger z2 = (byte)5.3F 也可以编译。是不是因为我试图转换一个编译时间常量而转换对它没有影响?

最佳答案

作为作业

整数 z=(byte)5

使用文字值,编译器会立即将其翻译成

Integer z = Integer.valueOf(5);

编译器不够智能,无法像这种情况那样引用变量:

Integer k= (byte)b;

关于java - 类型转换和编译时间常量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16344903/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com