Re: Memory Leaks & Strings

Discussion in 'Java' started by Steven, Aug 14, 2003.

  1. Steven

    Steven Guest

    "Chris Smith" <> wrote in message
    news:...

    > String.valueOf(preferredSize), but that object is short-lived and will
    > go away very soon. The primary advantage of String.valueOf is that it's
    > simpler and more straight-forward, not that it is faster.
    >


    preferredSize+"" will generally do this:

    String.operator+(preferredSize, "");

    Java will call an implemented function, called the +operator and will pass 2
    parameters of which 1 is of type double and the other one is an empty string
    which has the size of 1 stopbyte (0x00).

    the operator+() function will do this:

    convert preferredSize (type double) into a string:

    String pSize = String.valueOf(preferredSize);

    and then append the empty string of size 1 to the end of pSize.

    pSize.append("")

    and then:

    return pSize;

    (33 + "")

    will return "33"

    This is the same as String.valueof(33)

    this will return "33"

    Note that "33"[3] == 0x00 (stop byte)

    String.valueof(33) will definately be faster then (33+"") as (33+"") is
    essentially the same as String.valueOf(33), but you need to append an empty
    string to it.

    It will not be very much faster thought.

    > --
    > www.designacourse.com
    > The Easiest Way to Train Anyone... Anywhere.
    >
    > Chris Smith - Lead Software Developer/Technical Trainer
    > MindIQ Corporation
     
    Steven, Aug 14, 2003
    #1
    1. Advertising

  2. Steven

    Chris Smith Guest

    Steven wrote:
    > "Chris Smith" <> wrote in message
    > news:...
    >
    > > String.valueOf(preferredSize), but that object is short-lived and will
    > > go away very soon. The primary advantage of String.valueOf is that it's
    > > simpler and more straight-forward, not that it is faster.

    >
    > preferredSize+"" will generally do this:
    >
    > String.operator+(preferredSize, "");
    >
    > Java will call an implemented function, called the +operator and will pass 2
    > parameters of which 1 is of type double and the other one is an empty string
    > which has the size of 1 stopbyte (0x00).
    >
    > [... and more ...]


    I don't know where you're getting this from, but a lot of it is wrong.
    I'm allowing for your use of C++ operator overloading syntax just out of
    preference, though it's not very correct to say that String
    concatenation is implemented in a function, when the language spec
    clearly states that it will be replaced by a specific equivalent use of
    the StringBuffer API at compile-time without generating a method call to
    any method that's specific to String concatenation or that operates on
    the String data type.

    Nevertheless, it's incorrect to refer to a String as null-terminated in
    Java, and to say that the left-hand side of a String concatenation is
    converted into a String if it isn't one (it's actually converted into a
    StringBuffer, or perhaps just passed directly into StringBuffer.append
    of a StringBuffer that was created empty; either works fine).

    In the end, yes String.valueOf will probably be faster, exactly as I
    said (and you snipped) originally. However, you've got quite a few
    things wrong about the reasoning behind it, and it really just doesn't
    matter.

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

    Chris Smith - Lead Software Developer/Technical Trainer
    MindIQ Corporation
     
    Chris Smith, Aug 15, 2003
    #2
    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. Jon Skeet

    Re: Memory Leaks & Strings

    Jon Skeet, Jul 30, 2003, in forum: Java
    Replies:
    0
    Views:
    474
    Jon Skeet
    Jul 30, 2003
  2. Harald Hein

    Re: Memory Leaks & Strings

    Harald Hein, Jul 31, 2003, in forum: Java
    Replies:
    1
    Views:
    420
    Laura P
    Jul 31, 2003
  3. Steven

    Re: Memory Leaks & Strings

    Steven, Aug 14, 2003, in forum: Java
    Replies:
    1
    Views:
    444
    pete kirkham
    Aug 15, 2003
  4. Ben

    Strings, Strings and Damned Strings

    Ben, Jun 22, 2006, in forum: C Programming
    Replies:
    14
    Views:
    810
    Malcolm
    Jun 24, 2006
  5. Replies:
    4
    Views:
    156
Loading...

Share This Page