不多说,直接上干货!
TypeConvertDemo.java
//自动类型转换 class TypeConvertDemo { public static void main(String[] args) { //自动类型转换 byte b = 17; short s = b; int i = s; long L = s; float f =L; double d = f; //byte b2 = d; //错误 :从double转换到byte可能会有精度损失 System.out.println(d); //需求:强制把double类型的d转换为int类型 int num = (int) d; System.out.println(num); int num2 =(int)3.14; System.out.println(num2);//强制把double类型转换为int,输出3,精度丢失 int num3 = (int)123L; System.out.println(num3);//强制把long类型转换为int,输出123在范围内,没有发生精度损失 } }