数据类型转化的一些问题

数据类型转换一些问题

`//低-----------------------------------------------高
        //byte,short,char -> int -> long -> float -> double
        int i = 128;
        double b = i;

        //自动转换     低 -> 高

        int i2 = 128;
        byte b2 = (byte) i2; //内存溢出

        //强制转换   (类型)变量名   高 -> 低

        /*
        注意点
        1. 不能对布尔值进行转换
        2. 不能把对象类型转换为不相干的类型
        3. 在把高容量转换为低容量时。强制转换
        4. 转换的时候可能存在溢出,或者精度问题!
         */
        System.out.println((int)23.43);  //23
        System.out.println((int)-45.324f);  //-45

        char c = 'a';
        int d = c + 1;

        System.out.println(d);
        System.out.println((char) d);

        //操作比较大的数的时候,注意溢出问题
        //JDK7新特性,数字之间可以用下划线( _ )分割
        int money = 10_0000_0000;
        int years = 20;
        int total = money*years; //计算的时候溢出了
        long total2 = money*years; //默认是int,转换之前已经存在问题

        long total3 = money*((long)years); //先把一个数转换为Long

        //long类型数字后  L     l   尽量大写
posted @ 2022-11-25 23:07  我的博客日记  阅读(33)  评论(0)    收藏  举报