String literal and String Object

Discussion in 'Java' started by Prakash Prabhu, Aug 27, 2003.

  1. Hi,

    Is there any performance benefit of using a String literal instead
    of creating a new String Object , like , for eg :

    String str1 = "Hello I am here "; //String literal

    instead of

    String str3 = new ("Hello I am here "); //String Object

    Don't both str1 and str2 point to objects on the heap , since in Java
    most of the binding is done at run time?

    Thanks,
    Prakash
     
    Prakash Prabhu, Aug 27, 2003
    #1
    1. Advertisements

  2. "E.C. B├Ąck" <> scribbled the following:
    > String are weird objects. As far as I know,


    > str1 = "Thing"; and
    > str2 = new String("Thing");


    > are very different. The former makes one new string object and assigns a
    > reference to str1. The latter makes a new string object, twice, and assigns
    > a reference to str2. Strings are immutable; don't make too many of them.


    The new operator *ALWAYS* creates a new instance, which takes up space
    independently from the other instances, no matter what those instances
    are.
    If you type:

    String[] foo = new String[1000];
    for (int i=0; i<1000; i++) foo=new String("Thing");

    you are guaranteed to end up with 1000 different String objects. If, on
    the other hand, you type:

    String[] foo = new String[1000];
    for (int i=0; i<1000; i++) foo="Thing";

    then depending on the JVM settings, you might end up with only one
    String object, being referred to by 1000 references.

    --
    /-- Joona Palaste () ---------------------------\
    | Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
    | http://www.helsinki.fi/~palaste W++ B OP+ |
    \----------------------------------------- Finland rules! ------------/
    "I am not very happy acting pleased whenever prominent scientists overmagnify
    intellectual enlightenment."
    - Anon
     
    Joona I Palaste, Aug 27, 2003
    #2
    1. Advertisements

  3. Prakash Prabhu

    ?????? Guest

    literal will point to the same object ...
    new will create a new object although the word is the same ...
    so ... you should use new only you want to ...

    "Prakash Prabhu" <> wrote in message
    news:qS43b.17$...
    > Hi,
    >
    > Is there any performance benefit of using a String literal instead
    > of creating a new String Object , like , for eg :
    >
    > String str1 = "Hello I am here "; //String literal
    >
    > instead of
    >
    > String str3 = new ("Hello I am here "); //String Object
    >
    > Don't both str1 and str2 point to objects on the heap , since in Java
    > most of the binding is done at run time?
    >
    > Thanks,
    > Prakash
    >
     
    ??????, Aug 27, 2003
    #3
  4. Joona I Palaste wrote:
    > you are guaranteed to end up with 1000 different String objects. If, on
    > the other hand, you type:
    >
    > String[] foo = new String[1000];
    > for (int i=0; i<1000; i++) foo="Thing";
    >
    > then depending on the JVM settings, you might end up with only one
    > String object, being referred to by 1000 references.


    If you get anything different in this case then your JVM is out of
    compliance with the JLS (section 3.10.5).


    John Bollinger
     
    John C. Bollinger, Aug 27, 2003
    #4
    1. Advertisements

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. Replies:
    5
    Views:
    983
    Frank Schmitt
    Sep 22, 2003
  2. herrcho
    Replies:
    7
    Views:
    631
    Jack Klein
    Sep 26, 2003
  3. al

    String literal and const

    al, Dec 19, 2003, in forum: C Programming
    Replies:
    7
    Views:
    639
    Ben Pfaff
    Dec 20, 2003
  4. Replies:
    10
    Views:
    1,071
    Roland Pibinger
    Jan 24, 2007
  5. BiraRai
    Replies:
    2
    Views:
    327
    BiraRai
    Oct 31, 2008
  6. Tagore

    Pointer and string literal question

    Tagore, Dec 10, 2009, in forum: C Programming
    Replies:
    15
    Views:
    849
    Ben Bacarisse
    Dec 20, 2009
  7. 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:
    632
    Anonieko Ramos
    Sep 27, 2004
  8. iporter
    Replies:
    4
    Views:
    319
Loading...