java表达式运算

//表达式计算顺序都是从左到右
System.out.println(3 + 5 + " hello");	//8 hello
System.out.println("hello " + 3 + 5);	//hello 35
int a = 3;
int b[] = new int[5];
b[a] = a = 6;	//相当于b[3] = 6;
for(i = 0; i < b.length; i++) {
	System.out.println(b[i]);
}
		
a = 6;
a += (a = 9);
System.out.println(a);		//15
		
a = 6;
a = a + (a = 9);
System.out.println(a);		//15
		
a = 6;
int c = (a = 3) * a;
System.out.println(c);		//9
//不要在单个的表达式中对相同的变量赋值超过一次。
int j = 0;
for (i = 0; i < 100; i++)
	j = j++;
System.out.println(j);		//0

猜你喜欢

转载自jaesonchen.iteye.com/blog/2297981