Does ArrayList.clear () also delete all contained objects?

Assuming I have an ArrayList ArrayLists created this way:

ArrayList< ArrayList<String> > listOfListsOfStrings = new ArrayList< ArrayList<String> >(); 

If I call:

  listOfListsOfStrings.clear(); 

Will an attempt to access any of the lines inside listOfListsOfStrings later always result in a java.lang.NullPointerException error?

+6
source share
2 answers

No, only links will be cleared. If the reference to the object no longer exists, it may be garbage collection, but you will not receive NPE, since then you have no way to get a new reference to this object.

+3
source

No, it will not delete objects in an ArrayList if you still have external references to them. ArrayList.clear () does nothing for the objects referenced unless they are orphans, in which case you will not reference them later.

+1
source

Source: https://habr.com/ru/post/885939/


All Articles