Performance Tips and Tricks
I comparatively recently discovered the thing where StringBuffers are faster than Strings because of the reduced garbage collection. I was wondering if anyone here had any similar performance tips worth passing on. For example, are primitives any quicker or slower than their object versions? (int versus Integer, etc) how do arrays compare with the Collections API for performance?
Is there a VM that runs faster or smoother on a particular platform than any of the others?
Everything welcome...
[522 byte] By [
Breakfast] at [2007-9-26 3:14:08]

In the case of StringBuffer, the benefit is not only the gc. Strings are inmutable, so if you need to change it you need to create a new one, and object creation is a expensive task.
Because object creation is so expensive, it's important to decide when to create them. A bad example:
for (int i = 0; i < 10000; i++) {
String s = new String("Hi there");
System.out.println(s);
}
does the same thing as:
String s = new String("Hi there");
for (int i = 0; i < 10000; i++) {
System.out.println(s);
}
but much slower.
For mutable objects, sometimes is very usefull to create pools of objects that can be reused in your application, using factories to get the reference of an unused and previously created object. In the case of String, the String class maintains a pool that you can use if you need the same string frequently in your application, thru the intern() method.