Java' + ='复合运算符工作概念

时间:2017-07-04 10:44:23

标签: java operators classcastexception

我只是想知道它们之间有什么区别 a = a+b;a += b;

考虑,

int a = 10;
long b = 20;

a = a+b; // is not possible 

a += b; // is possible.

谢谢!

package com.test;


public class ClassCast {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int a = 1;
        long b = 2;

        /*Not possible. Compile time error.
        a = a+b;
        */

        //Possible. Why?
        //a += b;

        System.out.println(a += b);

    }

}

1 个答案:

答案 0 :(得分:0)

所以a + = b相当于a =(int)(a + b)。

由编译器完成的隐式转换