1

I am confused by how object references work. In the below example, at the end of the loop, Will result point to the myList.get(9) or myList.get(3)?

Class MyClass{
...
}

public static MyClass myUtilityMethod(){
    List<MyClass> myList = new ArrayList<MyClass>(10);
    //initialize the list
    ....
    ////////////
    MyClass tmp = null;
    MyClass result = null;

    for(int i =0; i < 10; i++){
        temp = myList.get(i);
        if(i == 3) result = temp;
    }

    return result;
}

Since result points to temp and temp changes in every loop iteration, does it mean that result will also change based on where temp is pointing?

faizal
  • 3,497
  • 7
  • 37
  • 62

4 Answers4

4

It will point to the object stored in myList at index 3 (as of when you ran that code).

The operation:

result = temp;

copies the value currently in temp into result. That value is a reference to the object that you just got from myList.get(i). There is no enduring link between temp and result.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • If i were to return `result` from a method, how would that work? Because `myList` exists only within the scope of the method. So `result` would be pointing to a non existing object? – faizal Oct 03 '14 at 13:00
  • 1
    @faizal: It would be fine. The objects don't *belong* to the list, the list just has a reference to them. What keeps an object in memory is whether anyone has a reference to it. So if you return `result`, whatever you're returning it to will have a reference to that object and so it won't be eligible for garbage collection. If `myList` was the only thing that had any ref to the other 9 objects, and nothing else had a ref to `myList` (and nothing does, in that code), then the `ArrayList` `myList` refers to becomes eligible for GC, as do the other nine objects, but not the one you return. – T.J. Crowder Oct 03 '14 at 13:04
  • 1
    Definitely worth looking at this: http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value – Davie Brown Oct 03 '14 at 13:27
1

Java object variables are references to objects. When a reference to an object is assigned; the content of the object itself is not copied.

In your case, the assignment of result happens only once, when i == 3. After the assignment, result references the object at location 3 of the list. Since there are no other assignments of result in the loop, result references the result of myList.get(3) call after the end of the loop as well.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

result doesn't actually point to temp. When you assign temp to result you are not assigning the object temp to result you are assigning the reference (pointer) to result. After the assignment result contains the same memory location pointer that temp did. It will be pointing to the same object that temp was when temp was assigned to result. Therefore result will be pointing to the object at index 3 of the myList List Object. It will not change as temp changes references because result is not pointing to temp it's pointing to the object that temp was pointing to when it was pointing to the List Object at index 3.

brso05
  • 13,142
  • 2
  • 21
  • 40
0

result will point to the same object temp pointed to when i was 3. This is the same object returned by myList.get(3).

Mureinik
  • 297,002
  • 52
  • 306
  • 350