new creates an object according to the constructor, and clone() creates a new object and initializes the fields with the contents of the original object.
I understand you are reading javadoc, so let me give you an example:
public class MyBaby implements Cloneable { int age = 0; String name = "Dolly"; List<String> list = new ArrayList<String>(); public static void main(String[] args) { MyBaby originalBaby = new MyBaby(); originalBaby.setAge(1); try {
The javadoc says:
this method performs a shallow copy of this object, not a deep copy.
which explains the behavior of our childrenโs list: links are copied, not elements that are referenced, so our copy is โshallowโ
The memory allocation may vary, of course:
- you can initialize fields in your constructor
- clone can initialize the field, i.e. an array
source share