`
hanlingbo2008
  • 浏览: 80218 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

表达式之谜---半斤(复合赋值表达式)

 
阅读更多

简单赋值操作符 =

复合赋值操作符 += , -=, *= ,/= , %= ,<<=,>>=,>>>=, &=,等

 

1. 复合赋值表达式自动将所执行计算的结果转型为其左侧变量类型。复合表达式会悄悄产生一个转型。

2. 不要将复合赋值操作符作用与byte,short,char类型变量。同样int型数的右边要确保不是long、float、double类型

 

代码:

public class Banjin {

	/**
	 * 半斤
	 */
	public static void main(String[] args) {
		short x = 0;
		int y = 123456;
		System.out.println(x+=y);//产生转型,将int
//		提示Type mismatch: cannot convert from int to short
		//System.out.println(x=x+y);
	}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics