String Literal Question

Discussion in 'Java' started by Mayor Curley, May 25, 2006.

  1. Mayor Curley

    Mayor Curley Guest

    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
    Mayor Curley, May 25, 2006
    #1
    1. Advertising

  2. Mayor Curley

    Alex Hunsley Guest

    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
    >
    Alex Hunsley, May 25, 2006
    #2
    1. Advertising

  3. Mayor Curley

    Eric Sosman Guest

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

    --
    Eric Sosman, May 25, 2006
    #3
  4. Mayor Curley

    Chris Uppal Guest

    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...
    Chris Uppal, May 25, 2006
    #4
  5. Mayor Curley

    Chris Smith Guest

    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. 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
    Chris Smith, May 25, 2006
    #5
  6. Mayor Curley

    Eric Sosman Guest

    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.

    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.

    --
    Eric Sosman, May 25, 2006
    #6
  7. Mayor Curley

    Chris Smith Guest

    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
    Chris Smith, May 25, 2006
    #7
  8. Mayor Curley

    Tony Morris Guest

    "Mayor Curley" <> wrote in message
    news:...
    > 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/
    Tony Morris, May 26, 2006
    #8
  9. Mayor Curley

    Chris Uppal Guest

    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
    Chris Uppal, May 26, 2006
    #9
  10. Mayor Curley

    Roedy Green Guest

    On 25 May 2006 10:14:00 -0700, "Mayor Curley"
    <> 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.
    Roedy Green, May 26, 2006
    #10
  11. Mayor Curley

    Mayor Curley Guest

    I would *guess* there must be some sort of compiler optimization and
    the compiler would not create 1 million objects.
    Mayor Curley, May 30, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Prakash Prabhu

    String literal and String Object

    Prakash Prabhu, Aug 27, 2003, in forum: Java
    Replies:
    3
    Views:
    632
    John C. Bollinger
    Aug 27, 2003
  2. Replies:
    5
    Views:
    732
    Frank Schmitt
    Sep 22, 2003
  3. herrcho
    Replies:
    7
    Views:
    421
    Jack Klein
    Sep 26, 2003
  4. Replies:
    10
    Views:
    769
    Roland Pibinger
    Jan 24, 2007
  5. Anonieko Ramos

    What's wrong with rpc-literal? Why use doc-literal?

    Anonieko Ramos, Sep 27, 2004, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    371
    Anonieko Ramos
    Sep 27, 2004
Loading...

Share This Page