我只是想知道它们之间有什么区别
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);
}
}
答案 0 :(得分:0)
所以a + = b相当于a =(int)(a + b)。
由编译器完成的隐式转换