【发布时间】:2011-02-12 19:49:32
【问题描述】:
我有以下代码sn-p。
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 处理使用变量的表达式,则必须进行强制转换
但是考虑到前面的摘要,为什么我需要转换第二个方法调用的参数呢?
【问题讨论】:
标签: java casting primitive-types