wood burning stoves 2.0*
The moose likes Java in General and the fly likes How do you guard your arrays? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "How do you guard your arrays?" Watch "How do you guard your arrays?" New topic
Author

How do you guard your arrays?

Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Hi,

If I return Collection I can return unmodifiable version from Collections class. Is there something for arrays? I know I can build another array and copy it, but maybe there is solution without performance price?

Vladas
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

No, but there is for Lists. If you need something to be unmodifiable, use one of the newer Collection classes rather than an array.


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

I think an unmodifiable Collection is the way to go.

But if you do end up cloning the array, then be sure that you make a "deep copy." Otherwise, the copied array will contain references to the same element objects as the original. (Yet another reason to go with the unmodifiable Collection.)


"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
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
But if you do end up cloning the array, then be sure that you make a "deep copy."

Which would mean we're not really cloning it, right? Cloning an array returns a shallow copy.

Otherwise, the copied array will contain references to the same element objects as the original. (Yet another reason to go with the unmodifiable Collection.)

Errr... no. That is, if we're talking about an array or collection of mutable objects, then Collections.unmodifiableCollection() is just as vulnerable to abuse as a cloned array is. Try this:

Using unmodifiableList() and similar methods only protects the collection itself from modification. If the objects inside the collection are mutable, then they are not protected. If you need to make absolutely sure the data in your original collection remains unmodified, you need a deep copy. Period.


"I'm not back." - Bill Harding, Twister
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

... Which would mean we're not really cloning it, right?

Yes, you're right. I was careless here -- should have said "copying" rather than "cloning."

...Collections.unmodifiableCollection() is just as vulnerable to abuse as a cloned array is...

Now, that I didn't realize. Thanks for pointing this out!
Vladas Razas
Ranch Hand

Joined: Dec 02, 2003
Posts: 385
Safe are only unmodifiable collections of immutable objects. It seems to me that this guarding is not practical. It will steal performance and also programmer time. So it must be done for secure systems, like banking software etc mostly. The best thing I came up with - trust people who use your code Unless of course you work with very sensitive data, as mentioned before.

Thanks!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How do you guard your arrays?
 
Similar Threads
Equality
Collection in oracle
query
generate random number into 2d arrays
static final array