public class Day1 {
public static void main(String[] args) {
//优先级:
//低------------------------------------->高
//byte,short,char->int->long->float->double
//强制类型转换 (类型)变量名 优先级:高-->低
int i=128;
byte b=(byte)i;
//内存溢出 byte:-128~127
System.out.println(b);
//自动类型转换 优先级:低-->高
double a=i;
System.out.println(a);
/*
注意事项:
1.不能对boolean值进行转换
2.不能把对象类型转换为不相干的类型
3.转换的时候可能存在内存溢出或精度的问题
*/
System.out.println("====================");
//JDK7新特性,数字之间可以用下划线分割
int money=10_0000_0000;
int years=20;
int total=money*years;
System.out.println(total);//-1474836480 计算的时候溢出了
long total1=money*years;
System.out.println(total1);//-1474836480 money和years默认是int,转换之前已经存在问题了
long total2=money*((long)years);//20000000000 先把一个变量转换为long
System.out.println(total2);
System.out.println("====================");
System.out.println((int)23.6);//23
System.out.println((int)-45.45f);//-45
System.out.println("====================");
char c='a';
int d=c+1;
System.out.println(d);//97
System.out.println((char)d);//b
}
}