1. Create an instance of a class without using the new keyword
When you create an instance of a class using the new keyword, all constructors in the constructor chain are automatically called. But if an object implements the Cloneable interface, we can call its clone() method. The clone() method does not call any class constructors. If you create an object using the Factory pattern, it is very simple to use the clone() method to create a new object instance.
2. Do not initialize variables repeatedly
By default, Java initializes variables to certain values when the constructor of a class is called. This should be especially noted when a class is derived from another class, because when an object is created using the new keyword, all constructors in the constructor chain will be automatically called.
3. Use exceptions with caution
Exceptions are bad for performance. Throwing an exception first creates a new object. The constructor of the Throwable interface calls the native method named fillInStackTrace(). The fillInStackTrace() method checks the stack and collects call tracing information. Whenever an exception is thrown, the jVM has to adjust the call stack because a new object is created during processing. Exceptions should only be used for error handling and should not be used to control program flow.
Pay attention to the following two points. (1) Avoid using try/catch for application logic. If it can be processed with logical statements such as if and while, try not to use try/catch statements as much as possible; (2) Reuse exceptions when exception handling is necessary. , reuse existing exception objects as much as possible. Because in exception handling, generating an exception object consumes most of the time.
4. Thread
Threads can make full use of system resources. While other threads are waiting for hard disk or network reading and writing, the program can continue to process and run. However, improper use of threads will also affect the performance of the program. Example: Use the Vector class correctly. Vector is mainly used to save various types of objects (including objects of the same type and different types). Vector provides thread safety protection functions. Even though many methods in Vector class are synchronized. But if you have confirmed that your application is single-threaded, synchronization of these methods is completely unnecessary. In addition, when searching for various objects stored in Vector, it often takes a lot of time to match types. When these objects are all of the same type, these matches are completely unnecessary. Therefore, it is necessary to design a single-threaded class or collection that saves objects of a specific type to replace the Vector class.
Regarding thread operations, please note: (1) Prevent excessive synchronization. If the program is single-threaded, be sure not to use synchronization. (2) Synchronize methods instead of synchronizing the entire code segment. Synchronizing a method has better performance than synchronizing the entire code segment.
5. Try to use local variables
The parameters passed when calling the method and the temporary variables created during the call are saved in the stack (Stack), which is faster.
6. Use static variables as much as possible
Let all instances share this variable.
7. Try to specify the final modifier of the class
Classes with the final modifier are not derived. java.lang.String, specifying final for the String class prevents people from overriding the length() method. In addition, if a class is designated as final, all methods of the class will be final. The Java compiler will look for opportunities to inline all final methods, which can improve performance by an average of 50%.
8. Object creation and allocating reasonable space and size to it
A large number of objects (or instances) are often generated in JAVA programming. Because the system not only takes time to generate objects, it may also take time to garbage collect and process these objects later. Therefore, generating too many objects will have a great impact on the performance of the program; in addition, many classes in JAVA have their default space allocation sizes. For the StringBuffer class, the default allocated space size is 16 characters. If the space size of StringBuffer used in the program is not 16 characters, then correct initialization must be carried out.
9. Use non-blocking I/O
Lower versions of JDK do not support non-blocking I/O API. To avoid I/O blocking, some applications create a large number of threads (in better cases, a buffer pool is used). This technique can be seen in many applications that must support concurrent I/O streams, such as Web servers. JDK 1.4 introduced a non-blocking I/O library (java.nio).
This article comes from the CSDN blog. Please indicate the source when reprinting: http://blog.csdn.net/Foxalien/archive/2009/12/18/5029659.aspx