In Java programs, apart from the Core algorithms, the major cause of performance problems is the code of the program itself. Therefore, optimization of program details can greatly improve the performance of the program.
1,
Make the final modifier of the class as much as possible, because the final modifier class cannot be derived, which not only prevents other classes from overwriting the key methods in the class, but if the class is specified as final, all methods in the class are of the final type, and the Java compiler will automatically inline all final methods.
2,
Switch case is more efficient than if else Mode
3,
Use local variables whenever possible. Local variables are in the stack, and the operation is fast and requires initialization. Other variables are created in the heap at a low speed.
4,
The jvm gc mechanism should not be too detailed. After the object is used, it is manually set to null.
5,
When using the synchronization mechanism, use the synchronous block instead of the code block for synchronization.
6,
Do not use try/catch in loop statements. Place it outside the loop. The exception should be used with caution, which is detrimental to the performance. when an object is thrown, a new object is created to collect the call information of the tracing stack.
7,
When using stringbuffer, use the appropriate capacity value for the first test. Stringbuffer maintains an internal character array. The default value is 16 characters. When the filled data reaches its maximum capacity, stringbuffer automatically increases its capacity to 2 times + 2. As long as the stringbuffer reaches its maximum capacity, you need to create a new character array and copy the old and new characters again (very expensive ).
8,
To copy a large amount of data, use the system. arraycopy () command.
9,
Multiplication and division are replaced by shift (left shift is multiplication 2 ).
10. Try not to use new
Keyword: Create a class instance
This is because all constructors in the constructor chain are automatically called when the new keyword is used to create an instance of the class. However, if an object implements the cloneable interface, you can call its clone method. The clone () method does not call any class constructor.