类型转换

  • Java是强类型语言,进行有些运算时,需要用到类型转换。

    低-------------------------------------------------->高

    byte,short,char->int->long->float->double

  • 运算时,不同类型的数据要转化同一类型,然后在进行运算

  • 手动强类型转换

    由高类型到低类型需手动转换

    输出结果大于本类型精度时要在运算前要对成分进行强类型转换

  • 自动类型转换

    在运算过程中包含高类型数据计算结果会自动按照高类型输出

public class Demo04 {
    public static void main(String[] args) {
        //操作比较大的数的时候,注意溢出问题
        int money = 5000_0000;  //数字间加下划线可方便计数,不影响输出值
        int years = 20;
        int total = money*years*12;  //-884901888,计算溢出
        long total1 = ((long)money) *years*12;  //要在计算之前解决好精度问题
        System.out.println(total1);
    }
}

posted on 2021-10-29 13:31  执酒提剑  阅读(28)  评论(0)    收藏  举报