This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Clone

 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).]
 
Ranch Hand
Posts: 175
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Jay Kay
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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)

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Everybody's invited. Except this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic