/* 强制类型转换:自动类型提升运算的逆运算 (大的数据类型转小的数据类型) 1.需要使用强转符:() 2.注意点:强制类型转换,可能导致精度损失 */ public class Demo12 { public static void main(String[] args) { //精度损失举例1 double d1 = 12.3; int i1 = (int)d1;//截断操作,损失小数点 System.out.println(i1); //没有精度损失 long l1 = 123; short s2 = (short) l1; System.out.println(s2); //精度损失举例2 int i2 = 128; byte b = (byte) i2; System.out.println(b);//-128 //===================================================================================== //变量运算规则的两个特殊情况 //1.编码情况①: long l = 123213; System.out.println(l); //编译失败:过大的整数 //long l2 = 12316546574987; long l2 = 12316546574987l; //====================================================================================== //编译失败 //float f1 = 12.3; //2.编码情况②: //整型常量,默认类型为int型 //浮点型常量,默认类型为double型 byte c = 12; //byte c1 = c + 1;//编译失败 byte c1 = (byte) (c+1); System.out.println(c1); //float f1 = c + 12.3;//编译失败 float f1 = c +12.3f; System.out.println(f1); } }