Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 72
Registered: ‎06-29-2009
My Device: Not Specified
Accepted Solution

Use of StringBuffer on BlackBerry

Hi there everyone,

 

I am trying to make sense of the documentation regarding best practices to employ when developing for the BlackBerry. One of the items touches on the issue of StringBuffers:

 

Avoiding StringBuffer.append (StringBuffer)

To append a String buffer to another, a BlackBerry® Java Application should use

net.rim.device.api.util.StringUtilities.append ( StringBuffer dst, StringBuffer src[, int offset, int length ] ). 

 

Now does this mean that one should not use StringBuffer in the following scenario.

 

StrigBuffer buffer = new StringBuffer();

buffer.append("Some String").append("Some other string");

 

or

 

buffer.append("Some String");

buffer.append("Some other String");

 

Or, hehehe, is this related to adding two StringBuffer together? There is also a code sample in the PDF (http://docs.blackberry.com/en/developers/deliverables/3831/JDE_4.5_Fundamentals_Guide.pdf - Page 17)

 

Code sample

public synchronized StringBuffer append(Object obj) {

if (obj instanceof StringBuffer) {

StringBuffer sb = (StringBuffer)obj;

net.rim.device.api.util.StringUtilities.append( this, sb, 0, sb )

return this;

}

return append(String.valueOf(obj));

}

 

In this, what does *this* refer to? All thoughts and suggestions are most welcome.

 

Thanks!

Schalk 

Developer
Posts: 16,786
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: Use of StringBuffer on BlackBerry

as long as you append strings to the stringbuffer you are fine.
appending another stringbuffer should be avoided, instead the stringutilities method should be used.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 168
Registered: ‎11-01-2008
My Device: Galaxy S II
My Carrier: Meteor (Ireland)

Re: Use of StringBuffer on BlackBerry

Also, you should avoid using string literals (or any final static string fields) anywhere in BB code. This is especially important for code that is called very frequently, such as in a loop. Use non-final static string fields instead.
Developer
Posts: 72
Registered: ‎06-29-2009
My Device: Not Specified

Re: Use of StringBuffer on BlackBerry

Thanks everyone, that is basically what I thought. Smiley Wink
Developer
Posts: 77
Registered: ‎06-03-2009
My Device: Not Specified

Re: Use of StringBuffer on BlackBerry

can you ellaborate on that please? what do you mean avoid using string literals?  and what should we use instead? i'm using alot of :

 

String test = "1234" +",";

 

and such...

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Use of StringBuffer on BlackBerry

@davidmccormack, Why should final static Strings be avoided? What benefit does a non-final static have over a final static when dealing with constants?

Highlighted
Developer
Posts: 131
Registered: ‎08-13-2008
My Device: Not Specified

Re: Use of StringBuffer on BlackBerry

Developer
Posts: 1,041
Registered: ‎07-16-2008
My Device: ಠ_ಠ

Re: Use of StringBuffer on BlackBerry

Developer
Posts: 168
Registered: ‎11-01-2008
My Device: Galaxy S II
My Carrier: Meteor (Ireland)

Re: Use of StringBuffer on BlackBerry

1. Start the JDE and debug something that uses static final strings in a loop.

2. Put a breakpoint before the loop and another after the loop.

3. When the first breakpoint is hit, use that JDE feature that allows you to save a snapshot of all the heap objects.

4. Run through the loop to the second breakpoint and get the delta between the two sets of heap objects.

 

You'll quickly see that there is one new string object in the heap for every place that a static final string is referenced within the loop. It shocked the heck out of me first time I did this!