class addCast { public static void main(String[] args) { short a,b,c; a=2; b=3; c=a+b; } }
//////////// ---------- javac ---------- addCast.java:8: 可能损失精度 找到: int 需要: short c=a+b; ^ 1 错误
Output completed (0 sec consumed) - Normal Termination
什么原因呢?
a和b相加以后返回的是int类型,需要强制转化为short c=(short)(a+b); 应该用 int 不能用short 没错 应该是 public class third { public static void main(String[] args) { int a,b,c; a=2; b=3; c=(short)(a+b); } }