Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > String Literal Question

Reply
Thread Tools

String Literal Question

 
 
Mayor Curley
Guest
Posts: n/a
 
      05-25-2006
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

 
Reply With Quote
 
 
 
 
Alex Hunsley
Guest
Posts: n/a
 
      05-25-2006
Mayor Curley wrote:
> 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.


>
> Thanks,
> Mayor
>

 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      05-25-2006


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);

--
http://www.velocityreviews.com/forums/(E-Mail Removed)

 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      05-25-2006
Mayor Curley wrote:

> 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...


 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-25-2006
Eric Sosman <(E-Mail Removed)> 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. 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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      05-25-2006


Chris Smith wrote On 05/25/06 15:59,:
> Eric Sosman <(E-Mail Removed)> 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.

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.

--
(E-Mail Removed)

 
Reply With Quote
 
Chris Smith
Guest
Posts: n/a
 
      05-25-2006
Eric Sosman <(E-Mail Removed)> wrote:
> Chris Smith wrote On 05/25/06 15:59,:
> > Eric Sosman <(E-Mail Removed)> 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
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-26-2006
"Mayor Curley" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.

--
Tony Morris
http://tmorris.net/


 
Reply With Quote
 
Chris Uppal
Guest
Posts: n/a
 
      05-26-2006
Chris Smith wrote:

> 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


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      05-26-2006
On 25 May 2006 10:14:00 -0700, "Mayor Curley"
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>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.

--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
compare string and "" string literal lovecreatesbea...@gmail.com C++ 10 01-24-2007 09:55 PM
What's wrong with rpc-literal? Why use doc-literal? Anonieko Ramos ASP .Net Web Services 0 09-27-2004 09:06 AM
String, String literal ? Could anyone explain to me ? herrcho C Programming 7 09-26-2003 04:54 PM
string literal as const string& parameter selder21@hotmail.com C++ 5 09-22-2003 11:50 AM
String literal and String Object Prakash Prabhu Java 3 08-27-2003 09:18 PM



Advertisments