package a0319v01;
public class a0319v01 {
public static void main (String[] args){
int score1 = 82;
double score2 = score1;
int score3 = (int)score2;
System.out.println("score1 = "+score1);
System.out.println("score2 = "+score2);
System.out.println("score3 = "+score3);
}
}
运行结果显示:
score1 = 82
score2 = 82.0
score3 = 82
请问,在编译 double score2 = score1; 这条语句时候,编译器是具体怎样进行的?为何score1类型转换后值没变?而后面强制类型转换 int score3 = (int)score2; 时候,score2内存里的数是如何让进行取舍,或者编译时候如何取整的?
double是有小数的,你原来的score1就是整数82,所以转成double后取成了小数82.0,后面double强制转换为int是直接将小数部分去掉,所以就又变回82了,而不是82.0