Original from: http://www-900.ibm.com/cn/support/viewdoc/detail? Docid = 2611083l05000
When writing Java applications, the following are the basic principles:
- Use a string buffer instead of a string connection. When performing consecutive string operations, you must avoid unnecessary creation of objects that must eventually be recycled.
- Avoid writing data to the Java console consecutively to reduce the cost of string operations, text formatting, and output.
- If necessary, use the primitive type of the variable to avoid the cost of object creation and operation.
- Frequently cache used objects to reduce the required amount of garbage collection and avoid repeated object creation.
- Group local operations as much as possible to reduce calls to Java local interfaces (JNI.
- You can only use the synchronization method when necessary to limit the number of tasks in the JVM and operating system.
- Unless necessary, avoid calling the garbage collector. If you have to call it, do so only in idle time or some non-critical stages.
- An integer may be used instead of a long integer, because 32-bit operations are faster than 64-bit operations.
- If possible, declare the method as final. The method for JVM to process final is better.
- When creating a constant, use the keyword static final to reduce the number of times the variable needs to be initialized.
- Avoid unnecessary "casts" and "instanceof" references, because the destruction operation in Java is not executed at runtime during compilation.
- When the array can meet the requirements, avoid using vectors as much as possible.
- Add and delete items from the end of the vector to get high performance.
- Use the-O option to compile the Java file.
- Avoid allocating objects in a loop.
- Use the buffer I/O and tune the buffer size.
- Use the connection pool and prepare the cache statement for database access.
- Connect to the database using the connection pool and try again instead of opening or closing the connection repeatedly.
- Maximize thread lifetime and minimize thread creation and destruction cycles.
- Minimize competition for shared resources.
- Minimize the creation of short-lived objects.
- Avoid remote method calls.
- Use callback to avoid blocking remote method calls.
- Avoid creating an object that is only used to access one method.
- Try to keep the synchronization method out of the loop.
- Stores string and character data in unicode format in the database.
- Record classpath so that the most common library appears first.
Common:
When adding strings, use the append method of stringbuffer to replace the "+" operator of string;
Determine the object that is no longer used and set its reference to NULL;
The combination of objects rather than class inheritance is preferred;
Try to create fewer objects to complete the same function;
Replace the instanceof keyword with polymorphism;
Use the final keyword to set short methods to "inline"
Avoid using vectors when arrays meet requirements.