# 一行的多个分配不按预期工作[英] Multiple assignment on one line not working as expected

### 问题描述

```int x = 4;
int y = 3;

System.out.println(x);
System.out.println(y);

x ^= y;

System.out.println(x);
System.out.println(y);

y ^= x;

System.out.println(x);
System.out.println(y);

x ^= y;

System.out.println(x);
System.out.println(y);
```

```int x = 4;
int y = 3;

System.out.println(x);
System.out.println(y);

y ^= (x ^= y);

System.out.println(x);
System.out.println(y);

x ^= y;

System.out.println(x);
System.out.println(y);
```

```int x = 4;
int y = 3;

System.out.println(x);
System.out.println(y);

x ^= (y ^= (x ^= y));

System.out.println(x);
System.out.println(y);
```

## 推荐答案

```x = x^(y = y^ (x = x^y));
```

```x = 4 ^ (y = 3 ^ (x = 4 ^ 3));
```

## 编辑:

```int i = 1;
s = i + (i = 2) + i;
```

```s = 1 +    2    + 2;
```

## 其他推荐答案

```int i = 2;
int j = (i=3) * i;
System.out.println(j);
```

```    int a = 9;
a += (a = 3);  // first example
System.out.println(a);
int b = 9;
b = b + (b = 3);  // second example
System.out.println(b);
```

JLS指定两个打印应该产生12个，并且不允许生成6.即，因为BE的分配位于右侧，左侧b(或隐式左在+=操作中)，首先获取和保存该分配之前的值，只执行括号内的操作.

## 其他推荐答案

```x ^= (y ^= (x ^= y));   // initial statement

x = x^(y = y^ (x = x^y)); //equals to

() have the highest precedence

x = x^(y = y^ (x = 3^4)); // first highest precedence ()

x = x^(y = y ^ (x = 7)); // still the first x is 3

x = 4 ^(y = 3 ^ (x = 7));  // now 3 ^ 7 =4

x = 4 ^ 4;  // now  3 ^ 7 =4

x= 0;
```