Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the array index or the assigned value evaluated first? [duplicate]

Tags:

java

arrays

If I have the following code:

array[index] = someValue;

does the someValue or the index get evaluated first?

like image 347
tbodt Avatar asked Dec 02 '13 22:12

tbodt


People also ask

What is the starting value of an array index?

An array element is one value in an array. An array index is an integer indicating a position in an array. Like Strings, arrays use zero-based indexing, that is, array indexes start with 0.

What is the starting index of a matrix or array select one?

array indices starting from 0.

What really is the purpose of the array index?

The INDEX function returns a value or the reference to a value from within a table or range. There are two ways to use the INDEX function: If you want to return the value of a specified cell or array of cells, see Array form.

Is the starting index of an array *?

So array index starts from 0 as initially i is 0 which means the first element of the array.


1 Answers

The index is evaluated first. See JLS section 15.26.1, in particular:

15.26.1. Simple Assignment Operator =

...

If the left-hand operand is an array access expression (§15.13), possibly enclosed in one or more pairs of parentheses, then:

  1. First, the array reference subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason; the index subexpression (of the left-hand operand array access expression) and the right-hand operand are not evaluated and no assignment occurs.

  2. Otherwise, the index subexpression of the left-hand operand array access expression is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and the right-hand operand is not evaluated and no assignment occurs.

  3. Otherwise, the right-hand operand is evaluated. If this evaluation completes abruptly, then the assignment expression completes abruptly for the same reason and no assignment occurs.

TL;DR: The order is 1[2]=3

like image 82
Jason C Avatar answered Nov 05 '22 04:11

Jason C