Java, "a [1] * = a [1] = b [n + 1]" does not work as expected?

So, as the name implies, I have a function that uses a temporary array, and I want to write a value from another array into it, and then multiply the two values ​​by myself.

Example:

float[] a = {0, 0}

a[0] *= a[0] = b[n    ];
a[1] *= a[1] = b[n + 1];

I would expect the above to do the following:

a[0] = b[n    ];
a[0] *= a[0]; //(aka: a[0] = a[0] * a[0])

a[1] = b[n + 1];
a[1] *= a[1];

Although this behavior is not like what is happening. Instead, it simply multiplies by the fact that the original value contained in “a” was with any value contained in “b” as follows:

a[0] = a[0] * b[n    ];
a[1] = a[1] * b[n + 1];

I always understood that everything that comes after evaluating "=" is first evaluated, as you can see, when you do:

float a, b;
a = b = 5;
//"a" and "b" both equal "5" now.

Anyway, wouldn't it show that my original example should work?

- , , , ?

+4
4

, . a *= b. , . JLS ( ):

.

, :

. , ; .

, . , .

. , .

, (. 5.1.13) ( set), .

:

a[0] *= a[0] = b[n    ];
  • a[0] say tmp
  • a[0] = b[n] , b[n] ( a[0] b[n])
  • tmp * a[0]
  • a[0]

, a[0] *= b[n].

: : , JLS, IMHO ( Java-). right - ** * assThe JLS :

12 ; - ( ). , a = b = c a = (b = c), c b, b a.

+7

Java :

, , ; .

a[0] *= a[0] = b[n ]; , a[0] b[n], a[0] . , a[0] *= b[n].

, .

+3

( ) Java (). , :

a[0] *= a[0] = b[n];

:

a[0] *= (a[0] = b[n]);

b[n], a[0]. :

a[0] = a[0] * b[n]

*= = . .

+2

, , , , , *= , .

, :

E1 op= E2
    <=>
E1 = (T) ((E1) op (E2))

op= - - *=, += ..

:

a[0] *= a[0] = b[n    ];

:

a[0] = a[0] * (a[0] = b[n]);

a[0] (a[0] = b[n]) - - .

:

  • a[0] ( "A" )
  • It reads the value from b[n](call it " B")
  • He assigns B a[0](*)
  • And then multiplies A * B(call it " C")
  • And then saves Cback to a[0].

So, this is equivalent to multiplying what is in a[0]by b[n], because the step marked with (*) above is redundant: the value assigned to it is never read before it is reassigned.

0
source

Source: https://habr.com/ru/post/1695139/


All Articles