public class thirteen { public static void main(String[] str){ System.out.println("数据类型转换"); //基本数据类型之间的运算规则 //1.自动类型提升 //当容量下的数据类型变量与容量大的做运算时 结果字段提升为容量大的类型 // byte 、char 、 short --> int --> long -->float --> double // 当byte 、char 、 short 做运算时 结果最少要拿一个 int去接收 byte b1=12; int i2=13; int i3=b1+i2; System.out.println(i3); short s1 =153; double d1=s1; System.out.println(d1); /************特别的*****************/ // char 类型 是可以来做运算的 char c1 ='a'; //97 int i4=10; int i5=c1+i4; System.out.println(i5); short s3= 10; //char c3 =c1+s3; //编译不通过 // short s4= c1+s3; //编译不通过 //2.强制类型转换 自动类型提升的逆运算 // 需要使用强转符 : () // 强制类型转换有可能导致损失精度 double d6 =12.3; // int i7 = d6; //编译失败 //有的时候需求 必须要用int 型 int i7 =(int)d6; //这种强制类型转换 并不是四舍五入 只是把小数点后面省去 //这种 损失精度 System.out.println(i7); //这种转换 不会有精度损失 long l1 =123; short s8 = (short) l1; System.out.println(s8); //但是如果数据过大 也会导致short 存不下 也算精度损失 // 具体细节跟二进制有关 我也不太懂 反正会出现这种问题 int i0=129; byte b12= (byte) i0; System.out.println(b12); //long 后缀 都要加 一个 l // 如果你要忘记了 他一般默认会自动转成int long l12=123456; System.out.println(l12); //如果 long过大的 就必须要上 L //************ // 情况1 // 浮点型 必须要加后缀 f float f12= 12.3f; System.out.println(f12); // 情况2 byte b =12; // 对于整型常量 默认类型为 int型 // 对于浮点常量 默认类型为 double类型 // byte b123=b+1; 编译不过 // float f123= b + 123.2;编译不过 //字符串类型 String // 不属于基础数据类型 // 他属于引用数据类型 // String数据类型是可以和八种基础数据类型做运算 // 运算结果仍是 String int number=100; String numberStr= "学号:"; String str1= numberStr+number; System.out.println(str1); //练习 char c ='a'; // 97 int num =10; String str2= "hello"; System.out.println(c+num+str2); // 107hello System.out.println(c+str2+num); //ahello10 System.out.println(c+(str2+num)); // a10hello System.out.println((c+str2)+num); // ahello10 //练习2 // * * String Str1="*"; char t='\t'; System.out.println(Str1+t+Str1); // 字符串数字型 转整数型 的 话 必须要用函数强制转 } }