类的转换

时间:2021-03-16 14:07:25   收藏:0   阅读:0

类型转换

public class 类的转换 {
   public static void main(String[] args) {
?
       /*类型转换
        低----------------------高
        byte,short,char-int-long-float-double
        高到低强制转换,低到高自动转换
        */
?
       //强制转换
?
       int a = 128;
       byte b =(byte)a;  //内存溢出 byte范围 -127-127;
?
?
       System.out.println(a);
       System.out.println(b);
       System.out.println("=====================================");
?
       //自动转换
?
       byte c = 40;
       double d =(double)c;
?
       System.out.println(c);
       System.out.println(d);
?
       /*注意点
       1.布尔值不能转换
       2.不能对不相干的类型进行转换
       3.高转低强制转换,低转高不需要理
       4.转换的时候需要注意内存溢出,或者精度问题
?
        */
?
       System.out.println("=====================================");
?
       System.out.println((int)23.5);   //23
       System.out.println((int)-48.95f);  //-48
?
       System.out.println("=====================================");
?
       char c1 =‘a‘;
       int c2 = c1+1;
?
       System.out.println(c2);
       System.out.println((char)c2);
?
       System.out.println("=====================================");
?
       int money = 10_0000_0000; //jdk新特性,可以用下划线分割
       int years = 20;
       int total = money*years;
?
       System.out.println(total); //-1474836480 出现溢出问题
?
       long totala =money*((long)years);  //先把一个数转化为long类型
?
       System.out.println(totala);
?
  }
}
?

 

结果

类的转换
128
-128
=====================================
40
40.0
=====================================
23
-48
=====================================
98
b
=====================================
-1474836480
20000000000
?
进程已结束,退出代码为 0
?

 

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!