1

I'm a little confused on what it means about ArrayLists holding references to objects. Can someone give me an example on how this is shown in code? Also, can arraylist have an element that is a reference to itself? Thanks!

6
  • 1
    Do you generally know, what references are? Commented Feb 10, 2015 at 3:30
  • I believe a reference is an object referring to something.. for example.. String hello = new String(); hello is a reference to a string object. am i correct? Commented Feb 10, 2015 at 3:35
  • Tom's question is a good one, but I will say that in Java we don't usually distinguish between "references to objects" and "objects". If someone told me that a list held "references to objects", I would just take that as a reminder that if the object is changed outside of the list, so is the object in the list: they are the same object. Commented Feb 10, 2015 at 3:36
  • Is this a homework question? This is a pretty fundamental question. Commented Feb 10, 2015 at 3:38
  • @swordhunter2000 A reference refers to an object. So if you would call list.add(hello); and then String anotherString = list.get(0);, then hello and anotherString would both refer to the same String object, because they share the same reference. This also shows that a list won't copy the passed object of the add method. It just store its reference. And it will return that reference if you access it using get(index). Commented Feb 10, 2015 at 3:42

2 Answers 2

1

You have to start making distinctions between variables, values, and objects.

Variables are containers. They contain values.

Values can be of two types, primitive and reference. Reference values are pointers to objects.

Objects are data structures which also have behavior.

So in

Object var = new Object();

var is a variable. new Object() is a new instance creation expression that evaluates to a value of type Object, that value is a reference to an object of type Object. That value is then assigned to var.

You can then use var to invoke a method

var.toString();

The runtime environment will evaluate var, which produces a reference value, retrieve the referenced object, and invoke the method. You can reassign the value stored in var by doing

var = someOtherValue;

Now var will hold a new value.

An ArrayList uses an array behind the scenes. An array is a special object where its elements (which you can think of as fields) are themselves variables.

So

Object[] arr = new Object[10];

is an array of type Object which contains ten variables. These variables contain values of type Object. Again, these values are references. You can evaluate them or you can reassign them.

arr[3].toString();
arr[7] = "maybe a string";
arr[9] = arr[9]; // possible, but makes no sense
Sign up to request clarification or add additional context in comments.

Comments

0

It means instead of copying the object byte-for-byte, a reference to the location of memory where the object is stored is put in the list.

List<Object> objects = new ArrayList<Object>();
Object myObject = new Object();
objects.add(myObject);
// objects contains one reference to myObject

objects.get(0).modify();
// myObject modified

Note that primitive values (int for example) will be copied.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.