String[] files = {"a.doc, b.doc"}; VERSUS String[] files = new String[] {"a.doc, b.doc"};

Discussion in 'Java' started by Matt, Sep 16, 2004.

  1. Matt

    Matt Guest

    What's the differences between the following:

    String[] files = {"a.doc, b.doc"};
    and
    String[] files = new String[] {"a.doc, b.doc"};

    I think similar question as:

    String file = "a.doc";
    and
    String file = new String("a.doc");
     
    Matt, Sep 16, 2004
    #1
    1. Advertising

  2. Matt

    Will Hartung Guest

    "Matt" <> wrote in message
    news:...
    > What's the differences between the following:
    >
    > String[] files = {"a.doc, b.doc"};
    > and
    > String[] files = new String[] {"a.doc, b.doc"};


    > I think similar question as:
    >
    > String file = "a.doc";
    > and
    > String file = new String("a.doc");


    The difference is more like this:

    public String getFile() {
    String file = "a.doc";
    return file;
    }

    String a = getFile();
    String b = getFile();

    In the first case, a == b, in the second, they don't.

    With the arrays, I don't believe that Java will statically allocate the
    array using the first technique.

    Consider:

    public String[] getFiles() {
    String[] files = {"a.doc", "b.doc"};
    return files;
    }

    String a[] = getFiles();
    String b[] = getFiles();

    If Java statically allocated the array, then you would see this:
    a == b

    If it creates the array anew with every method call, then they'd be
    different.

    Regards,

    Will Hartung
    ()
     
    Will Hartung, Sep 16, 2004
    #2
    1. Advertising

  3. "Will Hartung" <> wrote in message news:<>...
    > With the arrays, I don't believe that Java will statically allocate the
    > array using the first technique.

    ....
    > If it creates the array anew with every method call, then they'd be
    > different.


    Actually both statements produce exactly the same byte code which
    creates a new array and fills it with String's. You can't have
    String[]'s in the class file constant pool, only String's.

    /Jesper Nordenberg
     
    Jesper Nordenberg, Sep 17, 2004
    #3
  4. (Matt) writes:

    > String[] files = {"a.doc, b.doc"};
    > and
    > String[] files = new String[] {"a.doc, b.doc"};


    The first variant can only be used non-method-local variable
    declarations. It's a compiler thing: The resulting code is the same.
     
    Tor Iver Wilhelmsen, Sep 17, 2004
    #4
    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. Matthew Louden
    Replies:
    1
    Views:
    6,949
    Scott M.
    Oct 11, 2003
  2. Russ

    script versus code versus ?

    Russ, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    2,508
  3. Christoffer Sawicki
    Replies:
    5
    Views:
    264
    Christoffer Sawicki
    Sep 2, 2006
  4. Paul Butcher
    Replies:
    12
    Views:
    730
    Gary Wright
    Nov 28, 2007
  5. Replies:
    2
    Views:
    470
    Thomas 'PointedEars' Lahn
    Mar 11, 2008
Loading...

Share This Page