12

Suppose my code goes like this:

ArrayList list = new ArrayList();
Student s = new Student();    // creating object of Student class

myList.add(s);      // Here am confused ...

/* myList contains just the reference variable to the Student object, OR

   myList contains the actual Student object (memory allocation for name, rollNo etc)  ??   
*/

In Short when adding objects to ArrayList using add():

ArrayList is a list of "References to objects" or its a list of "actual objects" ???

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
Mustafa
  • 123
  • 1
  • 1
  • 8

9 Answers9

16

In Java, you never pass around actual objects. You are always dealing with a reference, which is essentially just an address to a location within memory where your object is stored.

Since you never work with actual objects, ArrayLists contains arrays of references to objects stored somewhere else (a place in memory called the heap).

Johan Henriksson
  • 687
  • 3
  • 10
4

ArrayList stores references to objects. I would advise you to use the generic version of ArrayList. Your declaration would be:

ArrayList <Student> list = new ArrayList<>();

You would benefit from type checking at compile time.

Also read http://en.m.wikipedia.org/wiki/Object_copy for explanations about object copy concepts and the different strategies adopted by Java and C++.

Tarik
  • 10,810
  • 2
  • 26
  • 40
4

The objects are stored on the heap, not 'inside' the arraylist. The arraylist stores references to where there objects are found, as you say.

Zavior
  • 6,412
  • 2
  • 29
  • 38
3

Objects within the ArrayList themselves are stored on the heap. The ArrayList simply provides references to those objects and the ArrayList instance is also on the heap.

Object references, at the end of the day, are simply addresses to locations within memory where the object is stored. Hence, the ArrayList contains arrays of references to objects stored on the heap (or in memory).

blackpanther
  • 10,998
  • 11
  • 48
  • 78
  • And how can we dynamically add objects to myList? I dont kow how many objects will be created at runtime. Obviously we cant take s1, s2 ... during programming. Can dynamic allocation be achieved by creating array of reference variable s[] and using something like myList.add(s[i]) ... everytime a new object is created ?? – Mustafa Sep 17 '13 at 17:17
  • Please see: http://www.coderanch.com/t/493942/java/java/JVM-allocates-memory-heap-ArrayList I believe that when an ArrayList is created it initially gets 10 elements, but it gets more memory as the number of object references increase. – blackpanther Sep 17 '13 at 17:22
2

ArrayList stores the references of objects.

The snippet will explain to you

public static void main(String[] args) {

    ArrayList<Dog> a=new ArrayList<TEST.Dog>();
    Dog d=new Dog("AL");

    a.add(d);
    d.setName("AFTER");

    System.out.println(a);
}

Here we are changing the Dog object independently out side of the list and it is getting reflected to the List, hence a reference is being stored in the list.

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
kalakanhu
  • 167
  • 1
  • 5
1

I know this is more specific than necessary, but Java stores an Object's reference by value. I would explain it, but there is already a good article here: Java is Pass-by-Value, Dammit!.

Scott Stanchfield also added some extra clarification on stackoverflow/reference-or-value.

Community
  • 1
  • 1
Funny Geeks
  • 483
  • 5
  • 12
0

Depends on how you see it. But its always a reference.

Here is an example:

String niceText = "Hallo array List";
ArrayList<String> list = new ArrayList<String>();
list.add(niceText);
System.out.print(niceText + " = " + list.get(0));
// Output: Hallo array List = Hallo array List

niceText = "Goodby list";
System.out.print(niceText + " = " + list.get(0));
// Output: Goodby list = Goodby list

list.get(0) = "Still in here";
System.out.print(niceText + " = " + list.get(0));
// Output: Still in here = Still in here

list.add("New value");
System.out.print(niceText + " = " + list.get(1));
// Output: Still in here = New value
// But there is no referencing object anymore for index 1, it exist only in the list

But you can go much further, by cloning your object or pass it in different ways to other components of your app. But this has nothing to do with the array list, this is how java handels object instances and there visibility.

Rene M.
  • 2,660
  • 15
  • 24
  • This is incorrect the second part will print "Goodby list = Hallo array List" not "Goodby list = Goodby list" – Grant Dec 22 '16 at 15:46
0
        ArrayList<Dog> arrayList = new ArrayList<Dog>();
        Dog dogReference = new Dog("AL");
        arrayList.add(dogReference);

        System.out.println(arrayList.get(0)); //Dog@ObjectRef
        dogReference.setName("AFTER");

        dogReference = new Dog("NEWER");

        // This is still referencing old reference though we have re-initialized
        System.out.println(arrayList.get(0)); //Dog@ObjectRef
        System.out.println(arrayList.get(0).getName());     //AFTER
        System.out.println(dogReference.getName());        //NEWER

Initially, I was confused after this example as after re-initializing same variable arrayList still point to the older one. ArrayList uses Object[] to store the references.

Ravi Parekh
  • 5,253
  • 9
  • 46
  • 58
-1

I'll bite. This is obviously a question resulting from a larger issue about object references in Java. Perhaps it'll help to ask why you would need to know if your object is being stored by reference or by value.

That's probably the more interesting question to answer as most everything in Java, except primitives and a few others, are stored by reference. To be at a point where you're needing to think about why your thought process needs to know if something is by reference or value is more likely the point where you'll be able to resolve any issues that prompted you to have to ask this question.

Just curious.

subdigit
  • 431
  • 4
  • 9