Java program performance optimization

Source: Internet
Author: User
Tags switch case

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.