类型转换

tzc941243753 / 2023-05-03 / 原文

public class Demo4 {
public static void main(String[] args) {
int i = 128;
byte b = (byte) i;
System.out.println(i);//128
System.out.println(b);//-128强制类型转换 大-小
/*
注意点:
1.不能对布尔值进行转换
2.不能把对象类型转换为不相干的类型
3.在把高容量转换到低容量时,强制转换
4.转换的时候可以存在内存溢出或者精度问题
*/
char c = 'a';
int d = c+1;
System.out.println(d);
System.out.println((char) d);

//操作比较大的数的时候,注意溢出问题
//jdk7新特性,数字之间可以用下划线分割
int money = 10_0000_0000;
int years = 20;
int total = money*years; //-1474836480
System.out.println(total);
long total2=money*years;//-1474836480 默认是int,计算完后才会转换成long
System.out.println(total2);
long total3 =money*(long)years ;
System.out.println(total3);

}
}