It's not a secret anymore!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes clone() used on array and object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "clone() used on array and object" Watch "clone() used on array and object" New topic
Author

clone() used on array and object

Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125

Why line 7 compiled without error, while line 9 couldn't?
TIA


SCJP 1.4
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
One more question is about 'Shallow Copy' and 'Deep Copy', it says that the result of clone() is 'shallow copy', but I tried the following and found that carraycopy got the contents of carray, So what's the shallow and deep mean?

Thanks for all your thoughts.

person
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Object.clone() throws CloneNotSupportedException. Because it is a checked exception the compiler would prevent you from using clone() without either placing it within a try-catch clause that traps it, or declaring it with a throws clause in the containing method. Please read more on the clone method in the API.
The default implementation of clone in Object just make a shallow copy of the object: construct a new object of the same type and copies into its fields the same values that were stored by the fields in the original object.
Consider the elements of an array as the fields of the array object --regarding the clone operation. Thus the clone method on an array of type int will create another array containing the same number of elements with the same values in each of them --just copying the int values.
Now consider the following class:

if the clone method is invoked on an instance of such class the sb field of the copy will receive the value stored by sb in the original object, that is a reference to the StringBuffer instance. Thus the same StringBuffer object is pointed to by both sb fields, it is shared by the instances of MyClass. This is known as a shallow copy. In order to get a deep one the clone method should be overriden in MyClass to clone the sb field itself. In a deep copy the fields pointing to object are also cloned.
Bruce Eckel will teach you how to do that in
Thinking in Java
[ October 28, 2002: Message edited by: Jose Botella ]

SCJP2. Please Indent your code using UBB Code
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
And line 7 compiles without error because the compiler knows that all arrays are cloneable, and that cloning an array will never throw CloneNotSupportedException.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Thanks, Jose. That's clear now.
Hi Ron, do you mean that even if the prototype of the method, see clone(), has a throw clause, we don't necessary to do try/catch or declare a 'throws' in the upper level?
Thanks
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
The compiler acts as if arrays have an overriding clone() method that doesn't throw exceptions.
Salim Mohamed
Ranch Hand

Joined: Sep 19, 2002
Posts: 55
Jose,
------------------------------------------------
If the clone method is invoked on an instance of such class the sb field of the copy will receive the value stored by sb in the original object, that is a reference to the StringBuffer instance. Thus the same StringBuffer object is pointed to by both sb fields, it is shared by the instances of MyClass. This is known as a shallow copy.
------------------------------------------------
To verify this I wrote the following code :
import java.lang.*;
class ShallowClone implements Cloneable{
private String str= "Hello";
public static void main(String args[]) throws CloneNotSupportedException
{
ShallowClone s = new ShallowClone();
ShallowClone copyOfs = (ShallowClone) s.clone();
copyOfs.str= "Hello World";
System.out.println (" True object :"+s.str);
System.out.println (" True object :"+s);
System.out.println (" Cloned object :"+copyOfs.str);
System.out.println (" Cloned object :"+copyOfs);
}//End of main
}//end of class
It is returning :
True object :Hello
True object :ShallowClone@f4b6dd98
Cloned object :Hello World
Cloned object :ShallowClone@f4a2dd98
I hope it is not sharing or pointing to the same String object.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Welcome to the Ranch Salim.
But you modified the field after the clone operation, how could not they be different then?
[ October 30, 2002: Message edited by: Jose Botella ]
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Also, your example uses String, not StringBuffer. Since Strings are immutable, there is never any reason to copy them.
Salim Mohamed
Ranch Hand

Joined: Sep 19, 2002
Posts: 55
Thanks Ron. It was different because of String. I tried with Stringbuffer and here is the output :
True object :HelloWorld
True object :ShallowClone@f4aaceac
Cloned object :HelloWorld
Cloned object :ShallowClone@f48eceac
It doesnt matter whether the object is modified after the clone ... it still points to the same object.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Originally posted by Salim Mohamed:
[QB]
True object :HelloWorld
True object :ShallowClone@f4aaceac
Cloned object :HelloWorld
Cloned object :ShallowClone@f48eceac

Hi Salim, do you mean that you only change the 'String' to 'StringBuffer', then get the result above. When I try this, I got compile error since StringBuffer cannot be initilized without 'new'. Could you please paste the complete code boy here? Thanks a lot
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120

It doesnt matter whether the object is modified after the clone ... it still points to the same object.

Whether we are talking about an immutable object or not if you change the object pointed to by the field like copyOfs.str= "Hello World"; the result cannot be identical objects.
Yan Bai
Ranch Hand

Joined: Jul 21, 2002
Posts: 125
Thanks Jose. That's what I am wondering about.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone() used on array and object
 
Similar Threads
Inheritence
Array reference assignment
Interface
Regarding Casting between classes and interface
casting