Changing a Character-object without creating a new object (?)

Discussion in 'Java' started by Jesper Sahner, Jan 18, 2005.

  1. Hi!

    Is it possible to change a Character-object/StringBuffer-object
    without creating a new Character-object/StringBuffer-object?

    E.g. the following works fine:
    ch1=new Character('A');
    ch1=new Character(ch1.toLowerCase(ch1.charValue()));

    However a new object ch1 is created, which shouldn't be necessary in
    my opinion. If the "toLowerCase"-operation is performed many times in
    a loop, it could be expensive with a new object created every time.

    I imagine something like this pseudo-code should be possible:

    ch1=new Character(); // create object
    ch1.setValue('A'); // set value in existing object
    ch1.toLowerCase(); // change existing object

    My point is, that I want to create a Character-object once and for all
    and then alter it, without having to re-create it all the time.


    Regards,
    Jesper
    Jesper Sahner, Jan 18, 2005
    #1
    1. Advertising

  2. Jesper Sahner

    Chris Uppal Guest

    Jesper Sahner wrote:

    > E.g. the following works fine:
    > ch1=new Character('A');
    > ch1=new Character(ch1.toLowerCase(ch1.charValue()));
    >
    > However a new object ch1 is created, which shouldn't be necessary in
    > my opinion. If the "toLowerCase"-operation is performed many times in
    > a loop, it could be expensive with a new object created every time.


    The object wrappers are not mutable (by design).

    If you are bothered by object creation times at all, then why are you using the
    object wrappers for character data rather than "raw" char-s ?

    If (for some reason) you are stuck with the object representation, then you
    could always use a precomputed array[] of Character indexed by the charValue().
    (Incidentally, I don't believe that doing toLowerCase() on a
    character-by-character basis will give correct Unicode results in general,
    though it may be adequate for your requirements). Note that it is /possible/
    (I haven't checked the implementation) that Character.toLowerCase() already
    includes this optimisation (which is made possible by the fact that Characters
    are /not/ mutable).

    -- chris
    Chris Uppal, Jan 18, 2005
    #2
    1. Advertising

  3. Jesper Sahner

    Ryan Stewart Guest

    "Jesper Sahner" <> wrote in message
    news:...
    > Hi!
    >
    > Is it possible to change a Character-object/StringBuffer-object
    > without creating a new Character-object/StringBuffer-object?

    [...]
    Character and all other primitive wrappers are immutable by design, which means
    you cannot do what you're asking about. StringBuffer is exactly the opposite.
    This is what it was designed for. Read the docs.
    Ryan Stewart, Jan 18, 2005
    #3
  4. On 18 Jan 2005 02:28:07 -0800, Jesper Sahner wrote:
    > Is it possible to change a Character-object/StringBuffer-object
    > without creating a new Character-object/StringBuffer-object?
    >
    > E.g. the following works fine:
    > ch1=new Character('A');
    > ch1=new Character(ch1.toLowerCase(ch1.charValue()));
    >
    > However a new object ch1 is created, which shouldn't be necessary in
    > my opinion. If the "toLowerCase"-operation is performed many times in
    > a loop, it could be expensive with a new object created every time.
    >
    > I imagine something like this pseudo-code should be possible:
    >
    > ch1=new Character(); // create object
    > ch1.setValue('A'); // set value in existing object
    > ch1.toLowerCase(); // change existing object


    To answer your specific question, no you can't change the value of an
    existing Character.

    However what about this IMO much simpler alternative:

    ch1 = new Character(Character.toLowerCase('A'));

    Realize that toLowerCase() is *static* and doesn't actually need an
    existing Character object, as your code seems to imply.

    /gordon

    --
    [ do not email me copies of your followups ]
    g o r d o n + n e w s @ b a l d e r 1 3 . s e
    Gordon Beaton, Jan 18, 2005
    #4
  5. "Jesper Sahner" <> schreef in bericht
    news:...
    > Hi!
    >
    > Is it possible to change a Character-object/StringBuffer-object
    > without creating a new Character-object/StringBuffer-object?
    >
    > E.g. the following works fine:
    > ch1=new Character('A');
    > ch1=new Character(ch1.toLowerCase(ch1.charValue()));
    >
    > However a new object ch1 is created, which shouldn't be necessary in
    > my opinion. If the "toLowerCase"-operation is performed many times in
    > a loop, it could be expensive with a new object created every time.


    Yes, in the cases where it will be expensive, char variables can almost always
    be used instead. Where objects are needed, a simple char array of size 1 will
    be suitable.

    > I imagine something like this pseudo-code should be possible:
    >
    > ch1=new Character(); // create object
    > ch1.setValue('A'); // set value in existing object
    > ch1.toLowerCase(); // change existing object


    char ch1 = 'A';
    ch1 = Character.toLowerCase(ch1);

    or

    char[] ch1 = new char[1];
    ch1[0] = 'A';
    ch1[0] = Character.toLowerCase(ch1[0]);

    or


    char[] ch1 = new char[] {'A'};
    MyLib.toLowerCase(ch1);

    > My point is, that I want to create a Character-object once and for all
    > and then alter it, without having to re-create it all the time.


    My point is, you don't need an object of Character all the time.
    Boudewijn Dijkstra, Jan 18, 2005
    #5
    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. C-man
    Replies:
    9
    Views:
    642
    Roedy Green
    Apr 11, 2004
  2. Aaron Beall
    Replies:
    2
    Views:
    1,804
    Aaron Beall
    Sep 14, 2007
  3. J
    Replies:
    0
    Views:
    223
  4. helzer
    Replies:
    0
    Views:
    128
    helzer
    Oct 19, 2007
  5. hollyhawkins
    Replies:
    2
    Views:
    221
    Tad McClellan
    Dec 19, 2006
Loading...

Share This Page