我只是想知道两者之间有什么区别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);
}
}
所以 a += b 等价于 a = (int( (a + b(。
编译器完成的隐式转换