J2EE Checklists – 7

1.    Avoid creating objects in a loop.

2. Use String literals instead of String objects (created using the ‘new’ keyword) if the content is same.

3. Never create objects just for accessing a method

4. Whenever you are done with an object make that reference null so that it is eligible for garbage collection. 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. Use primitive data types rather than using wrapper classes.

6. Whenever possible avoid using class variables, use local variables since accessing local variables is faster than accessing class variables.

7. Use techniques such as lazy evaluation. 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.

8. Use integer as loop index variable.

9. Use System.arraycopy() to copy arrays.

10. Avoid method calls in loops .

11. It is efficient to access variables in a loop when compared to accessing array elements.

12. Compare the termination condition in a loop with zero.

13. Avoid using method calls to check for termination condition in a loop When using short circuit   operators place the expression which is likely to evaluate to false on extreme left if the expresion contains &&

14. When using short circuit operators place the expression which is likely to evaluate to true on extreme left if the expresion contains only ||.

15. Do not use exception handling inside loops.

16. Use String.intern() method if you want to add number of equal objects whenever you create String objects using ‘new’ key word

17. StringBuffer with proper initial size gives best performance for String concatenation if Strings resolve at run time

18. Use ‘transient’ key word for unnecessary variables that need not be read from/written into streams

19. Use buffered input streams and buffered output streams to increase performance

20. Use ArrayList in case you don’t need your operations on Vector to be synchronized. The reason being all the methods in Vector are synchronized, by default

21. Use StringBuffer rather than the string concatenation operator (+).

22. Use methods that alter objects directly without making copies.

23. String.equals() is expensive if you are only testing for an empty string. It is quicker to test if the length of the string is 0.

24. Null out references when they are no longer used so that garbage collection can reclaim their space

25. Use System.arraycopy() to improve performance.

26. Perform the loop backwards (this actually performs slightly faster than forward loops do). [Actually it is converting the test to compare against 0 that makes the difference].

27. Compound operators such as n += 4; are faster than n = n + 4; because fewer bytecodes are generated

28. Use networking timeouts, TCP_NODELAY, SO_TIMEOUT, especially in case of dying DNS servers

29. StringBuffer default size is 16 chars. Set this to the maximum expected string length

30. Re-use Hashtables by using Hashtable.clear().

31. Set the Hashtable size to be large enough to hold the expected elements. Use a prime number for table size.

32. Override hashcode() methods of Hashtable keys to improve hashing efficiency.

33. Use non-synchronized hash table classes.


Leave a comment

0 Comments.

Leave a Reply

  • Read this materials also:
  • J2EE Checklists – 6
  • J2EE Checklists – 4
  • J2EE Checklists – 3
  • J2EE Checklists – 5
  • J2EE Checklists – 1
  • Java Interview Questions 3
  • J2EE Checklists – 2
  • Advanced array techniques – Java tutorial
  • Java Interview Questions 1
  • JSP Declarations
  • Sitemap   Follow j2eetutorial on Twitter    

    Δ Top