The following method does not compile. Can you suggest why?
• Make changes to the line 'x = x + i;' without changing the order, to make the
method compile.
• The value output is '-7616'. Explain why it is negative. Also, explain how Java
sets an integer to negative or positive.
public void outputValue(){
short x = 0;
int i = 123456;
x = x + i;
System.out.println(x);
}
• Make changes to the line 'x = x + i;' without changing the order, to make the
method compile.
• The value output is '-7616'. Explain why it is negative. Also, explain how Java
sets an integer to negative or positive.
public void outputValue(){
short x = 0;
int i = 123456;
x = x + i;
System.out.println(x);
}
解决方案 »
- 求教一个关于Java虚拟机版本的小白问题
- java swing
- 请教两个问题
- 求助:我用ant 的ZIP工具打包,ZipEntry 名称用的中文名,在WINDOWS下打开文件,出现乱吗
- 关于URL编码
- exception的规划
- 聊天雏形模型问题 说啥也不能接收到信息了 J2SE
- 如何用jdbc函数返回select查询到的整个表
- 因工作需要?哪里还有JDK1.1.1版本下载啊?
- 请教对于jList,我按照帮助加入了双击事件,可是他的动作会被mouseClicked(MouseEvent e)里的代码屏蔽掉?那怎样才能各做各的??谢谢
- String初始化 之小牛角尖问题
- 求助servlet怎么配置,更具体点是web-xml怎么配置
这个问题只能把short类型换掉,换成int类型或比int类型大的。如long
short x = 0;
int i = 123456;
x = x + i;
System.out.println(x);
}转型错误 java是向上转型的的short能转换为int,但是int转换为short会报错的
int i
x + i; 类型会被转成int
但是你把int转换为short则必定错了
可以写成
x = (short)(x + i);