I’m curious about the object reference in java let’s say we have a Person object
Person object = new Person("Joe");
I put it in an ArrayList<Person>
ArrayList<Person> person_collection = new ArrayList<Person>();
person_collection.add(object);
I create a new collection of Person again, then copy the items of person_collection
ArrayList<Person> collection = new ArrayList<Person>(person_collection);
If I get the item from collection
Person p = collection.get(i);
Is p has a reference to person_collection ??
In this case,
pcontains in the initialization step at least all the elements ofperson_collection.So when you directly iterate over this newly created collection, you will get the same pointed
Personelements that the ones contains into the initial collection.By the way, as Java never use the pass-by-reference but always the pass-by-value method, references themselves are not the same but that does not prevent them to point to the really same objects.