Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - Order of Operations - Using Two Assignment Operators in a Single Line

What are the order of operations when using two assignment operators in a single line?

public static void main(String[] args){
    int i = 0;
    int[] a = {3, 6};
    a[i] = i = 9; // this line in particular
    System.out.println(i + " " + a[0] + " " + a[1]);
}

Edit: Thanks for the posts. I get that = takes values from the right, but when I compile this I get:

9 9 6

I thought it would have been and ArrayOutOfBounds exception, but it is assigning 'a[i]' before it's moving over the 9. Does it just do that for arrays?

like image 981
HSeldon Avatar asked Feb 25 '12 03:02

HSeldon


People also ask

How do you do multiple assignments in Java?

int a, b, c; You can also assign multiple variables to one value: a = b = c = 5; This code will set c to 5 and then set b to the value of c and finally a to the value of b .

Is multiple assignment possible in Java?

Java permits the use of multiple assignments in one statement. In such statements, the assignment operators are applied from right to left, rather than from left to right.

Can we use assignment operator in if condition in Java?

Using the assignment operator in conditional expressions frequently indicates programmer error and can result in unexpected behavior. The assignment operator should not be used in the following contexts: if (controlling expression)


2 Answers

= is parsed as right-associative, but order of evaluation is left-to-right.

So: The statement is parsed as a[i] = (i = 9). However, the expression i in a[i] is evaluated before the right hand side (i = 9), when i is still 0.

It's the equivalent of something like:

int[] #0 = a;
int #1 = i;
int #2 = 9;
i = #2;
#0[#1] = #2;
like image 184
Tom Hawtin - tackline Avatar answered Oct 01 '22 02:10

Tom Hawtin - tackline


As per the specs:

  • http://docs.oracle.com/javase/specs/jls/se5.0/html/expressions.html

15.26 Assignment Operators There are 12 assignment operators; all are syntactically right-associative (they group right-to-left). Thus, a=b=c means a=(b=c), which assigns the value of c to b and then assigns the value of b to a.

So, a[i] = i = 9; is the same as i = 9; a[i] = i;

Edit

Actually, that's not the case. Sample test class:

import java.util.Arrays;

public class Mkt {
  public static void main(String[] args) {
    int[] a = new int[10];
    int i = 5;
    a[i] = i = 9;

    System.out.println(Arrays.toString(a));
  }
}

Sample run:

$ javac Mkt.java && java Mkt
[0, 0, 0, 0, 0, 9, 0, 0, 0, 0]

Please refer to the other answer for more information. Basically:

  • a[i] = i = 9 is the same as a[i] = (i = 9), as = is right-associative
  • However, operand evaluation is left-to-right, as per this:

    15.7. Evaluation Order

    The Java programming language guarantees that the operands of operators appear to be evaluated in a specific evaluation order, namely, from left to right.

    It is recommended that code not rely crucially on this specification. Code is usually clearer when each expression contains at most one side effect, as its outermost operation, and when code does not depend on exactly which exception arises as a consequence of the left-to-right evaluation of expressions.

I copied the second paragraph which is very instructive here - rarely does it make sense to write confusing code like that.

I also find this worth checking out.

like image 20
icyrock.com Avatar answered Oct 01 '22 03:10

icyrock.com