• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

clone() used on array and object

 
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Why line 7 compiled without error, while line 9 couldn't?
TIA
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And line 7 compiles without error because the compiler knows that all arrays are cloneable, and that cloning an array will never throw CloneNotSupportedException.
 
Yan Bai
Ranch Hand
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler acts as if arrays have an overriding clone() method that doesn't throw exceptions.
 
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1056
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, your example uses String, not StringBuffer. Since Strings are immutable, there is never any reason to copy them.
 
Salim Mohamed
Ranch Hand
Posts: 55
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
Posts: 125
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jose. That's what I am wondering about.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic