harmony-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Nathan Beyer <ndbe...@apache.org>
Subject Re: [imageio] return a String array
Date Sun, 28 Mar 2010 05:47:44 GMT
What's the context of each class? Is one class public (javax.*) and
the other an internal class (org.apache.harmony.*)?

This isn't something that's unique to String arrays, nor arrays;
returning a copy of a field is a safety measure to ensure
immutability, thread safety and other properties that an API may want
to guarantee. Frequently, a public API will define certain classes as
immutable, so the cloning of arrays is necessary, as the array's
contents could be manipulated -- an array is not immutable.

-Nathan


On Sat, Mar 27, 2010 at 10:20 PM, Yang Lang <yanglang@gmail.com> wrote:
> Hi guys,
>
> When I am reading through ImageIO package’s source code, I found out there
> are two difference way to return a String[].
>
> For some methods, they call Arrays.clone() to clone a new string[] and
> return the new one while some other methods returning the original String[]
> directly.
>
> e.g.:
> 1.
> public String[] getFormatNames() {
>    return names.clone();
> }
>
> 2.
> public String[] getImageReaderSpiNames() {
>    return readerSpiNames;
> }
>
> I am wondering what the difference between these two usages is. For what
> kind of situations I need to clone a new array ?
>
> Thanks,
>
> Lang
>

Mime
View raw message