| Java中涉及byte、short和char类型的运算操作首先会把这些值转换为int类型,然后对int类型值进行运算,最后得到int类型的结果。因此,如果把两个byte类型值相加,最后会得到一个int类型的结果。如果需要得到byte类型结果,必须将这个int类型的结果显式转换为byte类型。例如,下面的代码会导致编译失败: class BadArithmetic {
 static byte addOneAndOne() {
 byte a = 1;
 byte b = 1;
 byte c = (a + b);
 return c;
 }
 }
 当遇到上述代码时,javac会给出如下提示: type.java:6: possible loss of precisionfound   : 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文件。
 char ccc=98;
 System.err.println( ccc);//out put is b
 
 | 
	posted on 2005-11-04 19:56 
R.Zeus 阅读(400) 
评论(0)  编辑  收藏  所属分类: 
J2SE