By default Java does Shallow Cloning, if an object is cloned and it has a String like this String s = new String("Hi");
then the cloned object will point to a new String object or it will have a reference to the previous object?
The question here is that does Java treat Strings different when cloning and creates a new object of it or not??
new
is used to create a String, it will create a new objectnew
can be intern
ed to assign to an existing entry in String pool or creates new entry if it does not exist.super.clone
without any additional code, then all the references are copied (assuming super
is Object)String
references inside the cloned object will share the existing String
objects pointed by original object