Basic Java Optimisation Hints

1. Minimise object creation and use of Strings

The Java programming style encourages you to create lots of little objects which don't hang around for terrible long

Especially true when working with strings

How many object in the following code ? ...

Perhaps the cardinal performance sin in Java is to create too many objects. The Java programming style encourages you to create lots of little objects which don't hang around for terrible long. This is especially true when working with strings, where the compiler will magically create many little objects for you. But it's bad practice, and it can really hurt you're performance. For example, take a guess how many objects are used by the following code snippet:

    String my_str = "date:   " + new Date() + '\n' + 
                    "user:   " + System.getProperties.getProperty("user.name") + '\n' +
                    "thread: " + Thread.currentThread().getName();

12 objects. You could be stung for as many as 18 objects by some compilers.

New statement is an expensive operation

Java objects aren't small (18 - 20 bytes, but more typically the minimum object size is 40 bytes + your data !)