Forums Register Login

object creation in ArrayLists

+Pie Number of slices to send: Send
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
1
+Pie Number of slices to send: Send
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));
+Pie Number of slices to send: Send
I'm I correct in saying that after line 10 there are 5 objects on the heap?

-Joey
+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
 

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.
"Don't believe every tiny ad you see on the internet. But this one is rock solid." - George Washington
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 938 times.
Similar Threads
Obtaining the digits of a integer.
Understanding Collections.sort()
Sorting Of Array From Even Number To Odd Number
Regarding sorting data in arraylist
Question about Random Number Generation...
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:35:53.