<分区>
案例 1
byte b = 7; // why don't I need to cast 7 to byte in this case? byte b = (byte)7;
System.out.println(b);
输出: 7
案例 2
static void fun(byte b) {
System.out.println(b);
}
public static void main(String []args) {
fun(7); // Compiler gives error because a cast is missing here.
}
输出:
编译错误:方法 fun(byte) 不适用于参数 (int)。
我的问题是:在情况 1 中,7
是如何从 int
隐式转换为 byte
,而在情况 2 中,它迫使程序员显式转换它?
7
还在byte
范围内。
请提出建议。