• Post Reply Bookmark Topic Watch Topic
  • New Topic

object creation in ArrayLists  RSS feed

 
Joey Dale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you put objects into an ArrayList, are you inserting a reference to the object, or are you making a copy of that object and inserting that object?

Here is some sample code that is not behaving as I would expect.



Here I am adding to list the immutable Integer object reference x and y.

Then I am changing x and y to reference to different objects, yet the x in the ArrayList != the Integer x. Can someone explain the gap in my expectations?

-Joey
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37513
554
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Reference. But it's not a reference to the variable "x", it is a reference to "0". Here's what happens in your code:

Integer x = 1; // use the autoboxed "1" (this happens to be cached, but that's not important right now)
Integer y = 2; // use the autoboxed "2"
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(x); // put "1" in the list
list.add(y); // put "2" in the list
System.out.println("x = " + list.get(0));
System.out.println("y = " + list.get(1));
x = 0; // set the variable x to the autoboxed "0". The list doesn't know this happened
y = 0; // set the variable y to the autoboxed "0". The list doesn't know this happened
System.out.println("x = " + list.get(0));
System.out.println("y = " + list.get(1));
 
Joey Dale
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm I correct in saying that after line 10 there are 5 objects on the heap?

-Joey
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are myriad objects on the heap, but if we're just concentrating on the relevant ones created by your code, there're two Integer instances and one ArrayList instance.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joey Dale wrote:I'm I correct in saying that after line 10 there are 5 objects on the heap?

-Joey


There will be a lot more than that. There will be Thread objects and Class objects and whatever else the JVM uses to manage execution of your program.

As far as what your code has put onto the heap, I see 2 Integer objects and 1 ArrayList object, from lines 6, 7, 8. Of course, that ArrayList object has an Object[] object to hold its data, and may have other objects it uses to maintain its state. In general, we can't know how many objects there are on the heap just be examining code.

If you're thinking the add() calls create additional objects, they don't. As Jeanne already pointed out, the just copy references to the existing Integer objects into the ArrayList.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!