• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

If I put an object twice into an ArrayList... Do I now have 2 different objects, or just 1 object?

 
Ranch Hand
Posts: 185
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's say I have the following:



Then I do:



Since List allows duplicates, we now have three "u1s" in our list, and only one "u5."

The question is: Do I now have 3 distinct "u1" objects, or is there just one u1 object and usersList simply contains references to this one object?

 
Bartender
Posts: 5898
57
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Marshal
Posts: 24586
55
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Mark Richardson wrote:If I put an object twice into an ArrayList...



As Carey implied, your question is off course to start with. In fact you don't put objects into ArrayLists. You put references to objects there. And hence Carey's answer becomes clear.

You never get to deal with objects at all in Java. When you write new Thing() what you get from that is a reference to a Thing object. If you assign that to a Thing-typed variable, then the variable contains a reference to that Thing object. You can only manipulate that Thing object via references which are contained in variables or array entries (or probably some other obscure places I can't think of right now). Nothing in your code ever contains a Thing object.
 
Mark Richardson
Ranch Hand
Posts: 185
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes so much sense. Thank you!
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!