类型转换

类型转换

 

public class Dmo01 {
   public static void main(String[] args) {
       int i=128;
       double b=i;   //内存溢出

       //强制转换   (类型)变量名     高-----低
       //自动转换       低----高
       System.out.println(i);
       System.out.println(b);


       /*
       注意点:
       1.不能对布尔值进行转换
       2.不能把对象转换成不相干的类型
       3.在吧打扰啦转换低容量的时候进行强制转换
       4,转换的可能存在内溢出 精度问题

        */
       System.out.println("======================================================");
       System.out.println((int)23.7);       //23
       System.out.println((int)-45.89f);     //-45


       System.out.println("======================================================");
       char c='a';
       int  d=c+1;
       System.out.println(d);
       System.out.println((char)d);



  }
}
public class Demo02 {
   public static void main(String[] args) {
          //操作比较大的时候     注意溢出问题
          //jdk7     新特性 数字之间可以用下划线分割
            int  money  =10_0000_0000;
            int  years =20;
            int tota1 =money*years;   //-147483 6480       计算的时候溢出
              long tora2 =money*years;   //默认是int转换之前就出问题了

        long toatl3=money*((long)years);  //先把一个数转换成long
         System.out.println(toatl3);
  }
}

posted on 2022-09-27 19:28  余尅  阅读(19)  评论(0)    收藏  举报

导航