Results 1 to 17 of 17
  1. #1
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default Confused About Object References

    The way I understand it is that, if you assign one object reference to another reference variable, they both refer to one object. So if you change one object, it will be reflected across the board. For example, if you have:

    Java Code:
    String str1 = "Hello";
    String str2 = str1;
    
    str1 += " World";
    
    System.out.println(str2);
    Won't this print "Hello World" ?

    If so, then can someone explain to me something? I have a group of arrays of objects, and I'm iterating through and copying their elements like so:

    Java Code:
    for (int i = 0; i < lines; i++)
    {
      records_ssort[i] = records_bsort[i] = records_isort[i] = records_qsort[i] = records_one[i];
    }
    After I finish this, I sort one array. Shouldn't that mean all the other arrays will all be sorted as well, since I'm copying object references?

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Quote Originally Posted by jtmoney0511 View Post
    The way I understand it is that, if you assign one object reference to another reference variable, they both refer to one object. So if you change one object, it will be reflected across the board. For example, if you have:

    Java Code:
    String str1 = "Hello";
    String str2 = str1;
    
    str1 += " World";
    
    System.out.println(str2);
    Won't this print "Hello World" ?
    Always the answer to this sort of question is: try it and see.

    The compound assignment operator += might be tricky. My gloss on that code would be:

    Java Code:
    String str1 = "Hello";  // assign str1 a reference to a "Hello" string
    String str2 = str1;     // assign str2 a reference to whatever str1 references
                            // ie make str2 and str1 reference the same thing
    str1 += " World";       // (a) Take what str1 refernences and concatenate (add) it to a " World" string
                            // (b) assign str1 a reference to this newly created concatenated string

    Pictorially the effect of this += is:

    Java Code:
    Before:              After:
    
                         (b) "Hello World"
      "Hello"                     ^              "Hello"
       ^  ^                       |                ^
      /    \             (a) "Hello"+" World"      |
     /      \                     |                |
    str1   str2                 str1              str2

    str1 has been reassigned by the +=, but no such reassignment has been made to str2 so it goes on referring to whatever it was referring to before.

    An analogy:

    Java Code:
    int x = 42;
    int y = x;
    x += 5;
    System.out.println(y);

    "Think of a number" (your thought refers to some arithmetical value)
    "Write it on this piece of paper" (the numeral you write refers to the same value)
    "Add 5 to the number you are thinking" (your thought now refers to a different value)
    "What does the piece of paper say now?"

    The references that are assigned to variables in Java are values.

  3. #3
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    Thanks for your reply. OK String was a bad example, I guess. What I'm asking is, if you assign two variables to point to the same object, and then you change the object (instead of re-assigning the variable like I mistakenly did in my example), won't that change the object for both references?

    Here's a better example:

    Java Code:
    Integer[] arr1 = {1, 5, 2, 8, 64, 91, 4};
    Integer[] arr2 = arr1;
    
    bubbleSort(arr1);
    Arrays are objects, so:
    Won't arr2 be sorted as well when this is through?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jtmoney0511 View Post
    ...What I'm asking is, if you assign two variables to point to the same object, and then you change the object (instead of re-assigning the variable like I mistakenly did in my example), won't that change the object for both references?

    Here's a better example:
    ....
    Won't arr2 be sorted as well when this is through?
    To quote a great source: "Always the answer to this sort of question is: try it and see."

  5. #5
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    OK let me clarify a bit more.

    I have a Record class that I created.

    My constructor:

    Java Code:
      public Record(int num, int qty, String name)
      {
        part_num = num;
    	 quantity = qty;
    	 part_name = name;  
      }
    Now, if I create an array of 5 of these records, then another reference to the same array (arr1 and arr2), then sort one array, the other will also be sorted, right?

    Also, if I change the object stored in an element of one array, it will change the same element in the other array, I'm thinking...

    I need to get some rest, or I'd test this myself right now. You all don't have to reply right now. I'm just really confused with this stuff.

    Thanks!
    James

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jtmoney0511 View Post
    I need to get some rest, or I'd test this myself right now.
    Sigh,... which implies that your time is more important than ours. I know that this isn't your intent, so please do the right thing: test it and come back if you're still confused.
    Last edited by Fubarable; 10-17-2009 at 05:48 AM.

  7. #7
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    Yeah I don't think it implies that at all. All it implies is that, at the time in which I wrote that, I didn't have the energy or the mind-capacity to start testing code.

    Anyway, sorry to offend.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by jtmoney0511 View Post
    Yeah I don't think it implies that at all. All it implies is that, at the time in which I wrote that, I didn't have the energy or the mind-capacity to start testing code.

    Anyway, sorry to offend.
    Yeah, I probably overreacted. Sorry. Anyway, have you tried testing it yet? And if so, what have you concluded?

  9. #9
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    That depends on what your "bubbleSort" method does and how it does it.

    If you pass in an array, and the contents of that array are sorted and placed back into the same array, then yes, both will be sorted but if the method creates a new array and you assign the reference to this new array back to the original variable, then no, the second array reference will not point to a sorted array.

  10. #10
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    Yeah, I probably overreacted. Sorry. Anyway, have you tried testing it yet? And if so, what have you concluded?
    I did test it. In answer to my first question:

    "Now, if I create an array of 5 of these records, then another reference to the same array (arr1 and arr2), then sort one array, the other will also be sorted, right?"

    Yes, the "other array" does get sorted. I should have known this, as there are not really two arrays, but two references to the same object. My other question:

    "Also, if I change the object stored in an element of one array, it will change the same element in the other array, I'm thinking..."

    Again, the answer is, of course, yes.

    So instead of having two references to one array, I created a bunch of new arrays and copied the objects stored in those arrays:

    Java Code:
    for (int i = 0; i < lines; i++)
    {
      records_ssort[i] = records_bsort[i] = records_isort[i] = records_qsort[i] = records_one[i];
    }
    But here again, I figure that, since I sort one array, the other arrays would be sorted as well, since I've moved around the common objects in those arrays. This is not the case, though. That's what I'm still confused about.

  11. #11
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  12. #12
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

    Default

    I've read that before. It doesn't really help, but thanks!

  13. #13
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    The variables reference the arrays, not the arrays' elements. So, yes, the objects in the arrays are the same, but the arrays are not.

  14. #14
    Arnold is offline Member
    Join Date
    Oct 2009
    Location
    Rotterdam
    Posts
    52
    Rep Power
    0

    Default

    Try this:
    Java Code:
    str1 = "Hello"
    str2 = new String(str1 + " world")
    ..........
    the "new" part of the code forces the reference to refer to a new object, even if that object is the same as the other (at least that's what they say at school).

  15. #15
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    Java Code:
    Before:                      After:
    
    arr                 arr2     arr (sorted)        arr2    
     |                   |        |                   |    
     |                   |        |                   |    
     v                   v        v                   v    
     *-----> "fum" <-----*        *-\   />"fum" <-----*    
                                     \  |
                                      \/
                                      |\
     *-----> "fee" <-----*        *   | > "fee" <-----*    
                                   \  |
                                    | |
                                    \ /
     *-----> "foe" <-----*        *--\ -->"foe" <-----*    
                                    / \
                                    | |
                                   /  \
     *-----> "fie" <-----*        *    -> "fie" <-----*
    An alphabetic sort of an array (like that referenced by arr) changes the values of each of the array elements so that they reference different things.

    This has no effect on any other array whose elements happen to reference the same things in the same order to begin with.

    The reason is really much the same as that which started this thread because sorting is just a particular sequence of assignments. Assigning a value to a variable doesn't affect any other variable. Assigning a value to a slot in an array doesn't affect any other slot in any other array.

  16. #16
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default

    I figure that, since I sort one array, the other arrays would be sorted as well, since I've moved around the common objects in those arrays. This is not the case, though. That's what I'm still confused about.
    As the previous diagram was supposed to show sorting doesn't involve moving around a bunch of common objects. In Java it's all done with arrows. It's the arrows that are the reference values that are assigned to variables and array slots.

    Sorting really involves changing the arrows leaving the objects untouched and still able to be pointed to by the arrows of other variables and array slots.

  17. #17
    jtmoney0511 is offline Member
    Join Date
    Mar 2009
    Posts
    17
    Rep Power
    0

Similar Threads

  1. Multiple references to the same object
    By jlowery2663 in forum Advanced Java
    Replies: 6
    Last Post: 08-31-2009, 08:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •