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

J

Jesper Sahner

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
 
C

Chris Uppal

Jesper said:
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
 
R

Ryan Stewart

Jesper Sahner said:
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.
 
G

Gordon Beaton

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
 
B

Boudewijn Dijkstra

Jesper Sahner said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,141
Latest member
BlissKeto
Top