class badarithmetic {
static byte addoneandone() {
byte a = 1;
byte b = 1;
byte c = (a + b);
return c;
}
}
当遇到上述代码时,javac会给出如下提示:
type.java:6: possible loss of precision
found : int
required: byte
byte c = (a + b);
^
1 error
为了对这种情况进行补救,必须把a + b所获得的int类型结果显式转换为byte类型。代码如下:
class goodarithmetic {
static byte addoneandone() {
byte a = 1;
byte b = 1;
byte c = (byte)(a + b);
return c;
}
}
该操作能够通过javac的编译,并产生goodarithmetic.class文件。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!



