/*输出结果如下
1111
-1165059039
3.1415927
*/
public class test1 {
public static void main(String[] args){
long c =1111;
int a =(int)c; //强制类型转换,由于1111没有超过int的范围,所以没有产生溢出
long aa = 1111 * 1111 * 1111 * 1111; //因为超出int范围,所以产生溢出
int bb = (int)aa;
double pi =3.14159265479809999;//造成精度的损失
float f = (float)pi;
System.out.println(a);
System.out.println(bb);
System.out.println(f);
}
}
强制类型转换举例
最新推荐文章于 2021-05-23 03:38:34 发布
本文通过一个Java示例程序展示了不同类型之间的转换及其可能导致的问题,包括整数溢出和浮点数精度损失,并提供了具体的代码实现。
980

被折叠的 条评论
为什么被折叠?



