TestChange

public class TestChange {
public static void main(String[] args) {
//1.自动类型转换;
double d = 1;//double = int ;小->大
System.out.println(d);

//2.强制类型转换
int i = (int) 1.25;// int = double; 大->小需要强制转化
System.out.println(i);

//3.强制转换是的精度丢失和溢出
// float a = 0.123456789f;
// double b = a;
float a = (float) 0.123456789;//0.12345679 结果造成了精度丢失
System.out.println(a);

int x = 128;
byte y = (byte) x;
System.out.println(y);//-128 造成了溢出

//因此写程序时,选择合适的数据类型是解决问题的第一步!

//4.在多种类型参与运算,结果会向较大类型进行转换
int res =(int)(0.25f+1+0.75+2);//float+long+double+int==double

//5.对于byte、short、char、int、参与的运算,编译期会全部转为int在运算
short m=1;
byte n=2;
char c='中';
int res2=m+n+c;//m n c的类型转为int
System.out.println(res2);//20016
/*
总结:小转大自动转化
大转小强制转换
强制转换时会存在精度丢失和溢出
在运算时,结果会向较大类型自动转换
byte、short、char、int、参与的运算,编译期会全部转为int在运算
*/


}
posted @ 2022-03-20 21:45  涐啝雨の約定  阅读(39)  评论(0)    收藏  举报