Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning forum!
  • 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
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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

 
Ranch Hand
Posts: 186
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?

 
Saloon Keeper
Posts: 6258
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Sheriff
Posts: 24654
58
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: 186
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That makes so much sense. Thank you!
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!