String Literal Question

M

Mayor Curley

Hi, All:

I need some help on Java as I go through my code to make it more solid.

In this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
}

Now in this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
}

Thanks,
Mayor
 
A

Alex Hunsley

Mayor said:
Hi, All:

I need some help on Java as I go through my code to make it more solid.

In this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
}

One string.
Now in this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
}

One string. The key is that you write "stringX", and not:

new String("stringX")

- as calling new will create a new String object each time you call it.
 
E

Eric Sosman

Mayor Curley wrote On 05/25/06 13:14,:
Hi, All:

I need some help on Java as I go through my code to make it more solid.

In this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
}

(Didn't this question rumble past just a few days ago?)

The loop creates no String objects at all (unless
someMethod() creates them).
Now in this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
}

The loop creates no String objects at all (unless
someMethod() creates them).

The process of loading the class that contains these
code snippets will create one String object with the value
"stringX" (or will arrange to re-use an existing String
with that value). The loops then make one million and
five million references to that lone String, but create
nothing new in and of themselves.

Extra credit: How many String objects are created by

for (int i = 0; i < 1000000; i++)
someMethod("string" + i);
 
C

Chris Uppal

Mayor said:
Now in this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
}

There must be something strange in the air just now. Only a few days ago a
person (or persons) known as "John and Diane Curley" posted an eerily similar
question to this very group. You will easily find the resulting thread (with
the correct answer plus some explations) in Google's newsgroup archive[*]. The
thread title was "java compiler and string literals".

The answer, by the way, is zero.

-- chris


[*] In case you don't know how. Go to:
http://groups.google.com/advanced_search
Type
comp.lang.java.programmer
into the "Group" field (ignoring the crap that Google put there as an example),
and
"java compiler and string literals"
into the subject field, press "Search", and off you go...
 
C

Chris Smith

Eric Sosman said:
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. Also, are you counting any
String objects that may be created inside the implementation of
StringBuffer or StringBuilder?

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
E

Eric Sosman

Chris Smith wrote On 05/25/06 15:59,:
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.

Perhaps you're worried about what goes on in someMethod()?
I'd intended the same someMethod() that the original poster
used, so whatever his method does ...
Also, are you counting any
String objects that may be created inside the implementation of
StringBuffer or StringBuilder?

That was "the trick" in what I meant as a bit of a trick
question. I even considered asking about

for (int i = 0; i < 1000000; i++)
someMethod("string" + Integer.toString(i));

.... but decided that would be too broad a hint.
 
C

Chris Smith

Eric Sosman said:
Chris Smith wrote On 05/25/06 15:59,:

(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
 
T

Tony Morris

Mayor Curley said:
Hi, All:

I need some help on Java as I go through my code to make it more solid.

In this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
}

Now in this for loop how many String objects are being created?

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
someMethod("stringX");
}

Thanks,
Mayor

No (yes, none - you'll hear otherwise no doubt) String objects are being
created in the for loop.
One String object is created at class load time from the class'
constant_pool.
 
C

Chris Uppal

Chris said:
More plausibly, there's nothing to prevent Integer.toString(int,int)
from being implemented as: [..]

Nor anything to say whether Intege.toString() is relevant at all.

In point of fact, in Sun's 1.5.0, it is not. Aside: even
StringBuilder.append(boolean) is "inlined", which strikes me as overdoing it a
bit ;-)

-- chris
 
R

Roedy Green

for(int i = 0; i < 1000000; i++) {
someMethod("stringX");
}

"stringX" is created when the class is loaded. No other Strings are
created unless inside someMethod. A reference to "stringX" get pushed
to the stack for each call, but is still a reference to the same
literal object.
 
M

Mayor Curley

I would *guess* there must be some sort of compiler optimization and
the compiler would not create 1 million objects.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top