This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes clone( ) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "clone( )" Watch "clone( )" New topic
Author

clone( )

Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
If we had the following for example:

ArrayList<String> list = new ArrayList<String>();
...
...
...

ArrayList<Inbound> copy = (ArrayList<Inbound>)list.clone();

I know that it is a copy of the object.

But, my main point here is, what do we mean by "copy"? What do we actually copy?

Thanks.

[ May 28, 2008: Message edited by: Abder-Rahman Ali ]
[ May 28, 2008: Message edited by: Abder-Rahman Ali ]
Justin Fox
Ranch Hand

Joined: Jan 24, 2006
Posts: 802
Looks to me that yes you do copy, the object and it's
contents/attributes.

Justin Fox


You down with OOP? Yeah you know me!
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265

If you check the API for ArrayList.clone(), you'll learn it's a shallow copy. A new ArrayList object is returned, but it contains the same elements (not new copies) of the original ArrayList.

To see how this works, try this little program:



The output is:

list and copy are same object? false
item 0 is same object? true
item 0 is equals? true
item 1 is same object? true
item 1 is equals? true
item 2 is same object? true
item 2 is equals? true
// here we do String s = list.get(0); s = new String("different");
item 0 is same object? true
item 0 is equals? true
// here we did list.set(0, new String("new"));
item 0 is same object? false
item 0 is equals? false
[ May 28, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Also see the links in this recent thread.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Abder-Rahman Ali
Ranch Hand

Joined: May 22, 2008
Posts: 138
Thank you very much for your nice replies.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: clone( )
 
Similar Threads
Why doesn't the JUnit test OUTPUT appear using the Ant build?
Displaying an image issue
Decimal digits
for ( ; ;)
What does this mean?