Go Back   Velocity Reviews > Newsgroups > Java
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

Java - Returning array: reference or deep copy?

 
Thread Tools Search this Thread
Old 05-29-2004, 02:01 PM   #1
Default Returning array: reference or deep copy?


If a class contains a private array, and a public method of that class
returns that array using a return statement (e.g., return myArray),
will the caller get a deep copy of the array or a reference to the
encapsulated array? If it is the latter, is there a way to return the
array as a constant so that the caller cannot change the values on the
array?

Thanks!

Ken


Ken
  Reply With Quote
Old 05-29-2004, 02:06 PM   #2
Ryan Stewart
 
Posts: n/a
Default Re: Returning array: reference or deep copy?
"Ken" <> wrote in message
news: m...
> If a class contains a private array, and a public method of that class
> returns that array using a return statement (e.g., return myArray),
> will the caller get a deep copy of the array or a reference to the
> encapsulated array? If it is the latter, is there a way to return the
> array as a constant so that the caller cannot change the values on the
> array?
>
> Thanks!
>

Always references. If you want a copy (deep or shallow), you have to do it
yourself. And there is no such thing as a constant return.




Ryan Stewart
  Reply With Quote
Old 05-30-2004, 02:32 PM   #3
Michael Borgwardt
 
Posts: n/a
Default Re: Returning array: reference or deep copy?
Ryan Stewart wrote:

>>If a class contains a private array, and a public method of that class
>>returns that array using a return statement (e.g., return myArray),
>>will the caller get a deep copy of the array or a reference to the
>>encapsulated array? If it is the latter, is there a way to return the
>>array as a constant so that the caller cannot change the values on the
>>array?
>>
>>Thanks!
>>

>
> Always references. If you want a copy (deep or shallow), you have to do it
> yourself. And there is no such thing as a constant return.


Not with arrays, anyway. If you use a List, you can do it via
Collections.unmodifiableList().


Michael Borgwardt
  Reply With Quote
Old 06-01-2004, 04:43 PM   #4
John C. Bollinger
 
Posts: n/a
Default Re: Returning array: reference or deep copy?
Ryan Stewart wrote:

> "Ken" <> wrote in message
> news: m...
>
>>If a class contains a private array, and a public method of that class
>>returns that array using a return statement (e.g., return myArray),
>>will the caller get a deep copy of the array or a reference to the
>>encapsulated array? If it is the latter, is there a way to return the
>>array as a constant so that the caller cannot change the values on the
>>array?
>>
>>Thanks!
>>

>
> Always references. If you want a copy (deep or shallow), you have to do it
> yourself. And there is no such thing as a constant return.


The array classes all expose public clone() methods, however, so if a
shallow copy of the array is sufficient then

return (ElementType[]) myArray.clone();

Makes for a nice, simple, easy to read paradigm. If "ElementType" is a
primitive type then there is no distinction between shallow and deep
copying, and if it is immutable then performing a deep copy would be
wasteful. In some other circumstances a shallow copy is what you would
want anyway (you want the receiver to be able to mutate the returned
array elements with effects visible to the host object).

If you find that you really do want to make a deep copy of the array
then you might want to consider whether your design can be improved; it
is not necessarily so, but I wouldn't be surprised.


John Bollinger



John C. Bollinger
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
How to burn copy CD/DVD and create Data/Audio/Video Disc? dvdloveri Software 6 07-27-2009 07:58 AM
DVD Copy for Mac OS Users reallyone Software 2 03-22-2008 01:38 AM
Copy DVD Gold - a new software that can copy dvd heren DVD Video 4 10-12-2005 06:26 AM
Getting Around Copy Controls Ablang DVD Video 0 04-01-2005 06:09 AM
Dvd X copy express 3.0.1 Alexandre Magny DVD Video 2 09-12-2003 03:02 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46