aspose file tools*
The moose likes Beginning Java and the fly likes operand evaluation order Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "operand evaluation order" Watch "operand evaluation order" New topic
Author

operand evaluation order

Martyn Clark
Ranch Hand

Joined: Apr 16, 2005
Posts: 108
Hi i am having trouble understanding this bit of code:

int[] a = {10, 20, 30, 40, 50};
int index = 4;
a[index] = index = 2;

ok if i change the 2 to a value of 2 from anything from 0 to 3 i get the expected result but if i change it to 4 which i would have thought it would print the value 50 it does not it prints 4?
could any body be as kind as to explain why this is. the way i am understanding it at the moment is set index to 4 but on the last line we set it to 2 so it prints element two of the array which is 30.
Cheers..


Martyn...<br /> <br />SCJP 1.4 SCWCD 1.4
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
You're not expressing yourself very clearly, but the code speaks for itself:

Because of precedence, this last line is equivalent to the following, right?

When evaluating an assignment, the order is as follows:
  • The lhs is evaluated, and the l-value or location that is the

  • target of the assignment is remembered.
  • The rhs is evaluated and its value is remembered.
  • The actual assignment is done.

  • So in this case:
  • LHS a[index] is evaluated -- the assignment will be to a[4].
  • RHS (index = 2) is evaluated. Its value is 2 (with a side effect of

  • index being set to 2).
  • a[4] = 2

  • Does that make sense?


    There is no emoticon for what I am feeling!
    Martyn Clark
    Ranch Hand

    Joined: Apr 16, 2005
    Posts: 108
    Hi Gabby thanks for your reply,
    ok if im thinking write this means a[index] which evaluates index to 4 and on the rhs index = 2; which i take as now setting index to 2 which will print and does the value 30 but if i change this to index = 4; i dont get the result i would expect i get 4 instead of what i would have thought 50?
    Jeff Albertson
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 1780
    Sorry, I still don't understand what you are trying to write.
    Perhaps if we stick to code. For example, after executing:

    We have:

    Is it a certain result you are trying to achieve, or do you still
    not understand why this is the result?
    Martyn Clark
    Ranch Hand

    Joined: Apr 16, 2005
    Posts: 108
    Hi again sorry i am not explaing it too well here is the code i have:
    public class Test
    {
    public static void main(String[] args)
    {
    int[] a = {10, 20, 30, 40, 50};
    int index = 4;
    a[index] = index = 2;
    System.out.println(a[index]);
    }
    }

    Ok i think i have it this changes the value from element 4 from 50 to 2, what was throwing me was when i ran the code i got a result of 30 which is at element 2, obviously in the print statment i am saying print element 2 so when i changed the number from 2 to say three it would print 40 what i was missing if i am correct is that when assigning the number it would be just changing element 4 but in the print statment index would be changed to 3 but element 4 would now have value of 3! Sorry bit long winded but am i right?
    Jeff Albertson
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 1780
    I think we are in agreement, and that was some run-on sentence!
    Mark Spritzler
    ranger
    Sheriff

    Joined: Feb 05, 2001
    Posts: 17250
        
        6

    "Gabby Hayes"-
    Welcome to the JavaRanch! Please adjust your displayed name to meet the

    JavaRanch Naming Policy.

    You can change it

    here.

    Thanks! and welcome to the JavaRanch!

    Mark


    Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
    How to Ask Questions the Smart Way FAQ
    Jeff Albertson
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 1780
    Woops, didn't realize my namesake was a "celebrity"!
    How's the new moniker? And can I still keep the gif?
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24184
        
      34

    The name is now worse, because it's now obviously fake.

    The image is just fine, as is the first name. I went to elementary school with a "Gabby Smith". She was a lovely girl, although once she hit me so hard with her hairbrush the handle broke off.


    [Jess in Action][AskingGoodQuestions]
    Jeff Albertson
    Ranch Hand

    Joined: Sep 16, 2005
    Posts: 1780
    Okay, I'll let go of that moniker:

    Gabby Hayes |-> Gabby Haze |-> Jeff Albrechtsen

    Now that's got to be acceptable. It's certainly not "Jeff Albertson"
    Ernest Friedman-Hill
    author and iconoclast
    Marshal

    Joined: Jul 08, 2003
    Posts: 24184
        
      34

    Thank you, Jeff, for understanding. Welcome to the Ranch!
    Layne Lund
    Ranch Hand

    Joined: Dec 06, 2001
    Posts: 3061
    Martyn,

    It looks like you may have figured out the answer to your question. You might still find it helpful to print the WHOLE array. Run it several times with the value (2 in your original post) changed as you have been doing. This might give you some more insight into what is happening in your code.

    Layne


    Java API Documentation
    The Java Tutorial
     
    wood burning stoves
     
    subject: operand evaluation order