0

I have a question about using a LinkedList and the .get() operation. Java as I understand passes objects by reference, so if I have a linked list called A, and I do temp B = A.get(i), I retrieve an object B that I can modify and the changes are reflected in A.get(i).

However, if the object B has within it (Say another LinkedList object), I do not get a deep copy correct? Is the solution that I must create a copy constructor for my class 'temp' in this example. Or is there a better, built-in way to do this?

Thanks for your help.

August
  • 12,410
  • 3
  • 35
  • 51
Mike
  • 81
  • 7

3 Answers3

4

Java as I understand passes objects by reference

No. It passes references by value. It doesn't pass objects at all [except in the case of RMI.]

so if I have a linked list called A, and I do temp B = A.get(i), I retrieve an object B

No. You retrieve a reference that refers to B. The same B whose reference you passed when you added it to the list.

that I can modify and the changes are reflected in A.get(i).

Yes, see above.

However, if the object B has within it (Say another LinkedList object), I do not get a deep copy correct?

Correct. Just like the first case. No difference whatsoever.

Is the solution that I must create a copy constructor for my class 'temp' in this example.

Solution to what? I've never used a copy constructor or the clone() method in Java since 1997. What problem are you trying to solve?

Or is there a better, built-in way to do this?

To do what?

user207421
  • 305,947
  • 44
  • 307
  • 483
4

Java as I understand passes objects by reference..

No. Java pass everything by value. If you have a reference type, the reference is passed by value. See this question.

if I have a linked list called A, and I do temp B = A.get(i), I retrieve an object B that I can modify and the changes are reflected in A.get(i).

If you have a list of reference types get(i) will return a reference to a particular instance. The element in the list and your retrieved reference will refer to the same object. So if you change the object in some way, it will be "visible" from both references.

However, if the object B has within it (Say another LinkedList object), I do not get a deep copy correct?

Correct. You get a reference.

Is the solution that I must create a copy constructor for my class 'temp' in this example. Or is there a better, built-in way to do this?

If you need a deep copy of your object, you must implement it yourself.

Community
  • 1
  • 1
MAV
  • 7,260
  • 4
  • 30
  • 47
0

It does not return a "deep copy" because there is no copying done at all, at least, not at the object level. Let me explain. When you have an instance of an object, the variable that references that object is a pointer to the object. The object can be referenced and modified by many variables (pointers). Observe the following code:

// Let's assume I have a custom object class called Student
// Here the object is created and s now points to the new Student object
Student s = new Student();

// Here I create another variable that points to the same object
Student s2 = s;

Those two variables both point to the same object and any changes that one variable makes to the object will then be reflected in the other.

This ties into your list example. If you have a LinkedList of objects, it is actually a list of pointers to objects. So calling get(2) on the list will get a reference to the 3rd object in the list. The object that it's referencing is the object, not a copy. So any references, variables, methods etc. that were in this object will still be there.

I hope that answers your question :)

Rob
  • 305
  • 1
  • 8