Eric Sosman <> wrote:
> Chris Smith wrote On 05/25/06 15:59,:
> > Eric Sosman <> wrote:
> >
> >> Extra credit: How many String objects are created by
> >>
> >> for (int i = 0; i < 1000000; i++)
> >> someMethod("string" + i);
> >
> >
> > I'm afraid the answer to your extra credit question is undefined until
> > you specify some particular interpretation.
>
> (Ponders ...) I'm failing to discern the undefinedness.
> That doesn't mean undefinedness isn't present, just that it's
> escaping my eagle eye.
The undefinedness lies in the implementation of StringBuilder or
StringBuffer, and of Integer.toString(int,int). Put in the silliest
possible way, there is nothing to prevent all of the StringBuffer append
methods from being implemented as:
public StringBuXXXer append(...)
{
new String();
new String();
new String();
new String();
new String();
new String();
new String();
new String();
new String();
new String();
...
}
More plausibly, there's nothing to prevent Integer.toString(int,int)
from being implemented as:
private static final String[] dec = { "0", "1", "2", "3", "4" };
public static String toString(int val, int radix)
{
if ((radix == 10) && (val >= 0) && (val < dec.length))
{
return dec[val];
}
...
}
Do you see anything to prevent either behavior?
> Perhaps you're worried about what goes on in someMethod()?
That wasn't my concern.
--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation