【学习笔记】数据类型转换

143 阅读1分钟

强制转换:高容转低容 例如: int i =123; byte b= 1;//无法转换成功,需要进行下面起结构转换。 byte b = (byte)i; 在所转换的格式前加上(类型)变量名。

自动类型转换:低容转高容 例如: int i = 123; double b = i

数据类型转换注意点: 1.不能对布尔值进行转换; 2.不能把对象类型转换为不相干的类型; 3.把高容量转换为低容量,需要进行强制转换; 4.转换存在的问题有可能存在内存溢出,或者精度问题。

溢出问题: double a = 10; double b = 9; int GDP1 = Math.pow(a,b);//计算值溢出 long GDP2 = Math.pow(a,b);//转换存在问题 long GDP3 = (long)Math.pow(a,b);//转换为long System.out.println(GDP3); int years = 13; long total = GDP*years; System.out.println(total);