Skip to main content

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?


Comments

  1. = 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;

    ReplyDelete
  2. 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;

    ReplyDelete
  3. If I remember correctly, = operator is right-associative; so i will be assigned first, then a[i].

    ReplyDelete
  4. The = operator is right-associative(as others have already said). This can be easily proven with this test:

    int i = 2;
    int j = 3;
    int x = i = j;
    System.out.println(x); // This prints out 3.


    This works with all types, Objects and primitives.

    The way I have heard this referred to is "dual assignment", since, using the example above, you are assigning the value of j to both i and x.

    ReplyDelete
  5. What exactly do you espect to get as an output? There, you are assigning 9 to i and then the value of i to the array (but the array position was already calculed to the 0 position); so, since maybe you are confused about the array position in which the 9 is stored?... First you get a pointer to the a[0] (current value of i) and then, the compiler goes to process the rest of the sentence.

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.