Code sample
String str = "abc";
String str = "found " + n + " items";
Writing efficient loops
If your container is likely to contain more than one element, assign the size to a local variable.
If the order in which you iterate through items is not important, you can iterate backward to avoid the extra local variable on the
stack and to make the comparison faster.
Code sample
int size = vector.size();
for( int i = 0; i < size; ++i ) {
...
}
for( int i = vector.size() - 1; i >= 0; --i ) {
...
}
Optimizing subexpressions
If you use the same expression twice, use a local variable.
Code sample
int tmp = i+1; one( tmp ); two( tmp );
Optimizing division operations
Division operations can be slow on BlackBerry® devices because the processor does not have a hardware divide instruction.
When your code divides a positive number by two, use shift right by one (
>> 1
). Use the shift right (
>>
) only when you know
that you are working with a positive value.
Code sample
int = width >> 1;
Fundamentals Guide
Best practices for writing an efficient BlackBerry Java Application
15