byte(1),int(1),char(2),float(4),double(8) 在计算的时候会转换成所占字节大的类型;但int转换成flaot时由于有小数点的问题是不能转换的,同样的道理float转换成double也是不能转换的,但byte,char转化成int型时是可以转换的,但有时候会出错,因为在计算的时候是将结果直接截取一个字节(8位)来运算的,如果运算结果超过8位那截取的结果就会出问题。
如例:(如下例子有多处错误,请指出)
public class TestConvert {
public static void main(String[] args) {
int i=1,j=12;
float f1=0.1;
float f2=123;
long l1 = 12345678,l2=8888888888;
double d1 = 2e20,d2=124;
byte b1 = 1,b2 = 2,b3 = 129;
j = j+10;
i = i/10;
i = (int)(i*0.1);
char c1='a',c2=125;
byte b = b1-b2;
char c = c1+c2-1;
float f3 = f1+f2;
float f4 = f1+f2*0.1;
double d = d1*i+j;
float f = (float)(d1*5+d2);
}
}
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15450055/viewspace-539799/,如需转载,请注明出处,否则将追究法律责任。