我有以下代码片段。
public static void main(String[] args) {
short a = 4;
short b = 5;
short c = 5 + 4;
short d = a;
short e = a + b; // does not compile (expression treated as int)
short z = 32767;
short z_ = 32768; // does not compile (out of range)
test(a);
test(7); // does not compile (not applicable for arg int)
}
public static void test(short x) { }
以下总结是否正确(仅针对使用short的上述示例)?- 只有在使用文字或单个变量时才能进行直接初始化而无需转换(只要值在声明类型的范围内) - 如果赋值的rhs涉及使用变量的表达式,则需要进行转换
那么,根据前面的总结,为什么需要转换第二个方法调用的参数?