1. Use StringBuilder append method to concatenate strings instead of '+' operator. PSA program and see the result.
2. Use static final private methods wherever applicable. These methods are resolved faster than others .
3. Use String.intern method to improve performance.
4. Initialize values to defaults where ever required instead of setting it explicitly.
5. Use Long.valueof method instead of creating new Long object.
6. Catch specific exception instead of catching generic exception.
7. Avoid try/catch blocks in for loop.
8. Call equals method on static final string on dynamic string(CONSTANT.equals(dynamicString)).
9. Create collection with minimal size
Few more best java practices:
ReplyDelete10. Don’t create separate method for small job
Method call will be time consuming if method does small job.
The JVM needs to resolve the correct class of which method is called. This will be more issue if class hierarchy is long one.
If we really require a method then make it final so that it can be resolved at compile time itself.
11. Set temporary objects to null whenever we don’t require.
12. Use compound assignment operators like a+=b;
13. Try to create local variables instead of instance level variables. When you try to access instance level variable JVM internally invokes getField method.