From this SO-question, answers and comments I know that the result of
ArrayList<Object> listClone = new ArrayList<Object>(list);
does an extra step behind the scenes in comparison to
@SuppressWarnings("unchecked")
ArrayList<Object> listClone = (ArrayList<Object>)list.clone();
but the results are the same. So why is there even a Copy Constructor
when we have a Clone Method
?
NOTE: This question is mostly directed to the ArrayList
, since I know that it has both a Copy Constructor
and a Clone Method
. If there are any Java Objects that has either of them, but not both, then I wasn't aware of that. Still, my question is directed to the case of ArrayList
.
So, what is the purpose of it? In which case would you prefer to use the ArrayList's Copy Constructor
instead of its Clone Method
?
PS: If anyone has the exact code of both the Copy Constructor
and the Clone Method
of the Java#ArrayList
for comparison, that would be great.