Wednesday, 20 February 2013

Writing Performance Effective Java Code



Writing Performance Effective Java Code

Based on my prior experience and/or learning's, here are few tips I suggest every Java programmer to follow for writing performance effective Java Code.

  1. Use ‘StringBuilder’ instead of String concatenation: Always prefer the usage of ‘StringBuilder’ when you have requirement of concatenating large number of string values. 
  1. Do not prefer to use immutable class constructors; instead use static factory methods if available.  [eg: use of String constructor class for literals. Instead prefer the usage of String literals.] 
i.e. Prefer String s1=”abc”;    instead of     String s1=new String(“abc”);
      Use      Integer.valueOf(String)    instead of     new Integer(String);
       
  1. Prefer the use of primitive types instead of boxed primitive or wrapper types: 
Eg: Integer sum=0;
       for(int i=0; i<1000; i++)
             sum+=i;

The above program with wrapper type is much slower than this one:
int sum=0;
      for(int i=0; i<1000; i++)
             sum+=i; 

  1. Prefer the use of primitive data comparisons to String value comparisons.
    1. In primitives, prefer this comparison order. i.e. use boolean, byte, char, short, int, long instead of ‘float’ and ‘double’ comparisons 
  1. Minimize the usage of synchronization and synchronized utility classes in Java API’s wherever possible. 
eg. Prefer use of ‘StringBuilder’ to ‘StringBuffer’, ‘ArrayList’ to ‘Vector’, ‘HashMap’ to ‘Hashtable’ etc.
  
  1. Minimize the mutability of classes: Always declare the member variables or objects with least possible access specifier and make them ‘final’ when there is guarantee that it’s not changed. 
    1. Try to have all the member variables or objects as ‘private’ unless other access specifier is required
    2. Make all the unchanged primitive variables ‘final
    3. Make all the arrays or collections whose references should remain same as ‘final
    4. Make all the methods ‘final’ when you are sure that the method definition should not be changed.

  1. Keep re-usable code in static final methods: If there is any business logic, or functionality which does not belong to any of your classes or entity, move the methods into a utility class and make all of methods static and final. 
  1. Minimize the scope of local variables: Declare local variables only when it is used for first time. Do not declare them in the very beginning; and use it some where in the middle. By the time reader would have forgot the initial value of variable if any defined.  
  1. Use static block initializers to initialize or construct the expensive objects instead of having them in the methods which gets called many times. 
  1. Prefer the usage of interfaces to reflection: when you follow reflective way of instantiating your class i.e. (with use Class.forName(classname-to-load)), you 
    1. Lose all benefits of compile-time type checking
    2. Suffer with slow performance in method invocation.

Appreciate for taking time and reading through the different tips. If you would like to share your feedback or add on another tip, feel free to comment on the post.

No comments:

Post a Comment