5

I am new to java can some one tell me please. Is it

Shallow copy: primitive types and references are copied

Deep copy: objects are copied recursively

There is no default implementation for clone()

Dennis Kriechel
  • 3,719
  • 14
  • 40
  • 62
Simple-Solution
  • 4,209
  • 12
  • 47
  • 66
  • It does a shallow copy. – Ravi K Thapliyal Aug 07 '13 at 16:14
  • 3
    http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html#clone() – Nanne Aug 07 '13 at 16:17
  • 5
    I'll be vain here and quote myself from [earlier today](http://stackoverflow.com/questions/18095665/a-lambda-expression-with-a-statement-body-cannot-be-converted-to-an-expression-t/18095816#comment26488750_18095816): "What is with this **disease** that so many programmers seem to have where they refuse to take action based on reading error messages and they refuse to read [manuals](http://bit.ly/130yXJD) and language specs?" To wit: " Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation." *Seconds* of effort on Google. Vote to close for lack of effort. – jason Aug 07 '13 at 16:23
  • @Jason: People don't read manuals or language specs because the "cost" of asking a Stack Overflow question is much lower. Water and programmers flow downhill. – Gilbert Le Blanc Aug 07 '13 at 16:44
  • @Gilbert Le Blanc: That's why I said *seconds* of effort on Google. It's clearly *not* less cost. – jason Aug 07 '13 at 16:48
  • @Jason: Seconds of Google effort for you or me. Some people just can't formulate a good Google query. They ask on Stack Overflow in their own words, and a human does the Google query for them. There's definitely a need for this service, although its not Stack Overflow's purpose. – Gilbert Le Blanc Aug 07 '13 at 16:52
  • I agree with @Jason. The number of questions that can be resolved by a simple Google search is disappointing. GilbertLeBlanc: searching the title of this question (as posted by the OP himself) on Google reveals the answer *immediately* (first hit). – arshajii Aug 07 '13 at 17:08
  • @Gilbert Le Blanc: Come on. Typing "java object clone" is trivial. If he can type it in source, he can type it in Google. If he can type those words into his question, he can type it in Google. And he (or any other programmer with this terrible habit) *really* should learn to do it. Teach a man to fish.... – jason Aug 07 '13 at 17:15
  • FWIW, this question is the very first hit on my google search for 'java object clone default' – Travis Bear Mar 09 '16 at 17:29

4 Answers4

7

You can look at the documentation for clone():

The method clone for class Object performs a specific cloning operation. First, if the class of this object does not implement the interface Cloneable, then a CloneNotSupportedException is thrown. Note that all arrays are considered to implement the interface Cloneable and that the return type of the clone method of an array type T[] is T[] where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • Am I reading this correctly? If the object implements cloneable, then a call to `clone()` returns a shallow copy; if cloneable isn't implemented, a call to `clone()` returns a deep copy. – Muno Oct 23 '15 at 18:37
0

http://en.wikipedia.org/wiki/Object_copy -It's a shallow copy by default, but it is definitely possible to override it to perform a deep copy instead.

Song Gao
  • 666
  • 4
  • 14
0

According to Wikipedia

The default implementation of Object.clone() performs a shallow copy. When a class desires a deep copy or some other custom behavior, they must perform that in their own clone() method after they obtain the copy from the superclass.

Mateusz
  • 3,038
  • 4
  • 27
  • 41
0

If you want a quick and easy deep copy, use Apache's Lang package and their SerializationUtils.clone()

bluedevil2k
  • 9,366
  • 8
  • 43
  • 57