StringBuffer

I have read that strings are immutable and it is therefore inefficient to change their contents by doing something such as String string = new String("Hey!") and then attempting to reassign it a new value by doing something like string = "wassup?". I have read that the proper way to do this is to use a StringBuffer, which I have no problem with, except that in the app I am writing I need to assign the value in a string into a label over and over again with different values, which I have been doing currently as label.setText(string) and I haven't found a way yet to do the same thing using a StringBuffer. Am I missing something or is it not possible to assign the value of a StringBuffer to a JLabel? Thanks in advance.

[733 byte] By [ugga24a] at [2007-10-1 1:31:55]
# 1
StringBuffer has a toString method that will give you the contents of the StringBuffer as a String. Does that help?
tsitha at 2007-7-8 1:52:35 > top of Java-index,Security,Event Handling...
# 2

You're missing a couple of key concepts, and misunderstanding what you heard about Strings.

First, you should never need to use an explicit constuctor for Strings. String s1 = "abc";

Stirng s2 = new String("abc");

Both of the above do almost the same thing. The second one does create a new String object, and is less efficient, but that's miniscule. The main thing is that the second one is non-standard, harder to read, and adds no value.

> I have read that strings are immutable and it is

> therefore inefficient to change their contents by

> doing something such as String string = new

> String("Hey!") and then attempting to reassign it a

> new value by doing something like string = "wassup?".

Not quite. String's immutability means you can't change a String's contents.

String str = "abc";

str = "xyz";

str = foo.getName():

All of the above str = just stuff a reference value--a "pointer" to an object--into str.

> I have read that the proper way to do this is to use

> e a StringBuffer,

That's different.

String str s1 = "a" + foo.getSomething() + "b" + bar.getSometing(); // good

// bad, but see *** below

String s2 = "a";

s2 += foo.getSomething();

s2 += "b";

s2 += bar.getSomething();

// good

StringBuffer sb = new StringBuffer();

sb.append("a");

sb.append(foo.getSomething());

sb.append("b");

sb.append(bar.getSomething());

String s3 = sb.toString(); [/code]

***s2 is "bad" because each time you do s2 += whatever you're creating a StringBuffer, stuffing in the original s2, appending the other part, then creating a new String. You'd have to do a lot of it to make a noticeable difference.

Still, s3's append() would be better. But in that case, you could just go with s1 anyway.

The case where it would really make a difference would be in a loop where you don't have a fixed set of strings to put together at compile time, and the number could potentially get large.

s1 is fine because the compiler turns it into a bunch of calls to sb.append anyway, and the only String object created at runtime is the final result.

> which I have no problem with,

> except that in the app I am writing I need to assign

> the value in a string into a label over and over

> again with different values, which I have been doing

> currently as label.setText(string) and I haven't

> found a way yet to do the same thing using a

> StringBuffer.

You don't need to. Calling setText just copies the reference--a 4-byte value--so that the label's text property is pointing at the same string as the reference you pass in. It doesn't create a new string each time. (Well, it shouldn't. It's possible that the implementor of setText did something stupid like calling new String() on the parameter that's passed, but I doubt it.)

> Am I missing something

Several things, actually. :-)

> or is it not

> possible to assign the value of a StringBuffer to a

> JLabel?

If there's no method with that signature, then it's not possible. It's also not necessary, nor would it be beneficial if it were possible.

jverda at 2007-7-8 1:52:35 > top of Java-index,Security,Event Handling...
# 3

Just a comment on use of String vs StringBuffer.

use String for most things.

Use StringBuffer when

- you incrementally building up a large chunk of text (as jverd said, using StringBuffer.append(anotherString) is much more efficient than using String += anotherString)

- you need to actually modify the internal string. (ie insert characters, modify characters etc etc)

In your case it is neither. You are just changing the value of a label constantly.

Use a String.

evnafetsa at 2007-7-8 1:52:35 > top of Java-index,Security,Event Handling...
# 4
Thanks for the help! =)
ugga24a at 2007-7-8 1:52:35 > top of Java-index,Security,Event Handling...