数据类型 JAVA 示例 1.inti=178; 2.lomg|=8864L:(8864) 3. double d1=37266; 4. double d2=37.266D:(37.266d) 5. double d3 =26.77e3 6. float f=87363F:(87363f) 7. char c=d 8. boolean b1 true 9. boolean b2= false
11 数据类型 ◼ 示例 1. int i = 178; 2. long l = 8864L; (8864l) 3. double d1 = 37.266; 4. double d2 = 37.266D; (37.266d) 5. double d3 = 26.77e3; 6. float f = 87.363F; (87.363f) 7. char c = ‘d‘; 8. boolean b1 = true; 9. boolean b2 = false;
类型转换( Casting) JAVA 将一种类型的数据转换为另一种类型的齦据 操作数转换为同种类型,然后运算 整数型、实数型和字符型 ■表达形式:(类型)操作数 友用场合 1.二元运算符的二个操作数类型不同 2.表达式值的类型与变量的类型不同 两种方法 1.隐型类型转换:自动类型转换(系统宠成) 2.显型类型转换:强制类型转换
12 ◼ 将一种类型的数据转换为另一种类型的数据 ◼ 操作数转换为同种类型,然后运算 ◼ 整数型、实数型和字符型 ◼ 表达形式: (类型) 操作数 ◼ 应用场合 1. 二元运算符的二个操作数类型不同 2. 表达式值的类型与变量的类型不同 ◼ 两种方法 1. 隐型类型转换: 自动类型转换(系统完成) 2. 显型类型转换: 强制类型转换 类型转换 (Casting)
类型转换( casting) JAVA ■隐型类型转换:自动类型转换(系统宠成) ■宽化转换( widening conversion) byte j=60 short k=4; int =31; long m=4 long result=Ol result +=j-8 result *=k+2. result /=m+1 result-= result %=m
13 ◼ 隐型类型转换:自动类型转换(系统完成) ◼ 宽化转换(widening conversion) byte j=60; short k=4; int l=31; long m=4l; long result=0l; result +=j-8; result *=k+2; result /=m+1; result -=l; result %=m; 类型转换 (casting)
类型转换( casting) JAVA 隐型类型转换:自动类型转换(系统宠成) 类型转换表 源类型转换后不会丢失数据的目的类型 byte short, char, int, long float, double short char, int, long float, double char int, long float, double int long float, double one float double float ouble 14
14 ◼ 隐型类型转换:自动类型转换(系统完成) 类型转换表 源类型 转换后不会丢失数据的目的类型 byte short, char, int, long, float, double short char, int, long, float, double char int, long, float, double int long, float, double long float, double float double 类型转换 (casting)
类型转换( casting) JAVA 显型类型转换∶强制类型转换 ■窄化转换( narrowing conversion) double a=1.5 float b= System. out. println ( b=+ b) 编译:" possible loss of precision 数据精度丢失→数据丢失 double a=1,5: float b =( float )a; System. out println b=+ b) 15
15 ◼ 显型类型转换: 强制类型转换 ◼ 窄化转换 (narrowing conversion) double a = 1.5; float b = a; System.out.println(“b=" + b); 编译: “possible loss of precision” 数据精度丢失→数据丢失 double a = 1.5; float b = (float)a; System.out.println(“b=" + b); 类型转换 (casting)