Tips for J2Me and Java Development

Some tips that i found at the mutant's musings blog and i think it's very interesting for our knowledge (specially for developers):


  1. Avoid creating objects in a loop. i.e. Try to avoid create instance of a class in a loop.
  2. Use String literals instead of String objects (created using the 'new' keyword) if the content is same. JVM maintains an unique list of references for "interned" strings to avoid duplicate String objects in heap memory. So when you create a new string via String Literal, the JVM checks whether the String object exists, if yes, it would returns the reference and would not create a new String object, i.e. unique string objects. But if strings are created with "new", the JVM does not perform checking and would always create a new string object in heap. If you absolutely have to use "new" to create String objects, make sure you "intern" the strings, i.e. intern()
  3. Make used objects eligible for garbage collection. Whenever you are done with an object make that reference null so that it is eligible for garbage collection.
  4. Do not keep inheritance chains long. Never keep inheriting chains long since it involves calling all the parent constructors all along the chain until the constructor for java.lang.Object is reached.
  5. Accessing local variables is faster than accessing class variables
  6. Use lazy evaluation, lazy object creation whenever possible. Lazy evaluation refers to the technique of avoiding certain computations until they are absolutely necessary. This way we put off certain computations that may never need to be done at all. Lazy object creation allows us to delay the memory allocation to an object till it is not being put into use. This way a lot of memory is saved till the object is actually put in to use.
  7. Never create objects just for accessing a method.
  8. Use primitive data types rather than using wrapper classes.
  9. Whenever possible avoid using class variables, use local variables since accessing local variables is faster than accessing class variables.
  10. Use StringBuffer for string concatenation operation only if String resolves at runtime. Use + operator for concatenation gives best performance if Strings resolve at compile time.
  11. Avoid putting method calls, assigning values to variables, or testing for conditions in a loop. Try to put them outside the loop if you can.
  12. Avoid using method invocation as termination condition in a loop, try to use temporary variable for loop termination condition.
  13. Avoid using exception handling in a loop.
  14. When using short circuit operators place the expression which is likely to evaluate to false on extreme left if the expresion contains &&.
  15. When using short circuit operators place the expression which is likely to evaluate to true on extreme left if the expresion contains only ||.
  16. Use System.arraycopy() for copying array instead of using a loop.

0 comentários:

top