File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Clone 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" Watch "Clone" New topic
Author

Clone

Jay Kay
Ranch Hand

Joined: May 17, 2001
Posts: 58
class CloneTest
{
public static void main(String[] args)
{
int ia[ ][ ] = { { 1 , 2}, null };
int ja[ ][ ] = (int[ ] [ ])ia.clone();
System.out.print((ia == ja) + " ");
System.out.println(ia[0] == ja[0] && ia[1] == ja[1]);
}
}
What is the difference between ia and ja
Any help is greatly appreciated.
-Jay
Ragu Sivaraman
Ranch Hand

Joined: Jul 20, 2001
Posts: 464
Arrays are Objects in java
Here a shallow cloning is happening
ia and ja becomes two different arrays of array references
Thats why ia==ja becomes false and
ia[1] == ja[1] becomes true
Ragu
[This message has been edited by Ragu Sivaraman (edited September 23, 2001).]
Sasikanth Malladi
Ranch Hand

Joined: Nov 04, 2000
Posts: 175
How come that
int ia[ ][ ] = { { 1 , 2}, null };
is accepted by the compiler when null can't be cast or promoted to an int?
Shashi
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
since this is a bidimensionnal array of int, null stands here for the reference to the second array so this is allowed as far as I know. But I think that the following declaration would not be accepted by the compiler
int[][] array = {{1,2},{1,null}};
Val


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jay Kay
Ranch Hand

Joined: May 17, 2001
Posts: 58

Here a shallow cloning is happening
ia and ja becomes two different arrays of array references
Thats why ia==ja becomes false and
ia[1] == ja[1] becomes true
Ragu
Thanks Ragu.
Some follow up questions,
1. What is Shallow Cloning
int ia[ ][ ] = { { 1 , 2}, null };
int ja[ ][ ] = (int[ ] [ ])ia.clone();
2. How is ia[1] (which is null) = ja[1] (which is null) as two nulls cannot be equal
3. is ia[0][0]=ja[0][0] (is it 1) and
ia[0][1]=ja[0][1] (is it 2)

james hoskins
Ranch Hand

Joined: Jun 28, 2001
Posts: 31
hello jay,
shallow cloning is when you simply call the clone() method on an object which produces an exact copy of that object.
deep cloning happens when the object you call is an aggregate object which implements the Cloneable interface. this interface is a marker interface which tells you that when you call clone() on this aggregate object, it will recursively clone all the objects it contains references to.
kind regards,
james
Jane Griscti
Ranch Hand

Joined: Aug 30, 2000
Posts: 3141
Hi guys,
The Object.clone() method does a 'shallow copy'. It will copy all the fields of the cloned object; but if any of them are reference fields (non-primitive type variables) then only a copy of the reference is made. It will not create and copy the objects referenced..
For example, if a class has a field <code>Object myObject</code> the clone will reference the same 'myObject'. Any changes made to the cloned 'myObject' will be seen in the original 'myObject'.
If you want entirely new copies you must override the clone() method to create copies of the referenced objects.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
 
subject: Clone