package www.base;
/***
* boolean 1bit
* low---------------------------------------high
* byte < short < char < int < long < float < double
* 小数优先级高于整数,所以float比long高
* byte 1byte -128 ~ 127
* short 2bytes -32768 ~ 32767
* char 2bytes
* int 4bytes -2147483648 ~ 2147483647
* long 8bytes -9223372036854775808 - 9223372036854775808
* float 4bytes
* double 8bytes
*
* 高到低必须要强制转换
* 低到高不需要强制转换,自动转换。
*
* 注意
* 1,布尔值不能转换
* 2,高到低必须强制转换,低到高可以用强制转换。
* 3,转换时候可能存在内存溢出
* 4, 赋值时考虑定义类型的最大值,以免内存溢出
*/
public class Demo002_TypeConvert {
public static void main(String[] args) {
int i = 128;
byte b = (byte)i; // 强制转换,内存溢出
System.out.println(b);
System.out.println("==============================");
double d = 45.77;
float f = -56.98f; // 不加f会报错
System.out.println((int)d+" "+(int)f); //小数转整数,直接去除小数部分
System.out.println("==============================");
char c = 'c';
int i1 = c + 1;
System.out.println(i1);
System.out.println((char)i1);
System.out.println("==============================");
//JDK7 数字直接可以用下划线分割
int money = 10_0000_0000;
int year = 20;
int total = money * year;
long total1 = money * year;
System.out.println(total); //内存溢出
System.out.println(total1); //内存溢出,默认输出类型是和输入类型一致
long total2 = (long)money*year; //强制转换输入的int类型去long类型
System.out.println(total2);
}
}