Recently, I have been optimizing application performance and accumulated some rules and experience during the review code process. The goal of implementing these rules is to write "beautiful" code.
1. Make comments as comprehensive as possible
A Method annotation should contain detailed input parameters and result descriptions, and details should be given if an exception is thrown. A Class annotation should contain the class function descriptions, author, and modifier.
2. It is best to convert the same variable used for multiple times into a constant.
Variables with the same value used in multiple places should be summarized as a constant to facilitate future maintenance.
3. Execute method calls in the loop as few as possible
Try to avoid method calling in the loop, which can save the creation of the method stack. For example:
- for(int i=0;i<list.size();i++){
- System.out.println(i);
- }
It can be changed:
- for(int i=0,size=list.size();i<size;i++){
- System.out.println(i);
- }
4. Constant definitions can be placed in interfaces.
In Java, only constants are allowed in the interface. Therefore, you can save the public static final keywords by placing constants in the interface declaration.
5. Selection of arraylist and rule list
This problem is common. Generally, it is better for programmers to evaluate the use cases of the list and then make choices based on the features. The underlying layer of arraylist is implemented using arrays. Therefore, random data reading is much faster than sorted list, while sorted list is implemented using linked lists. Data addition and deletion are much faster than arraylist.
6. String, stringbuffer, and stringbuilder
This problem is also common. During String concatenation, strings usually generate multiple objects and cache multiple values in the constant pool. For example:
String a="a"; String b="b"; a=a+b;
In this case, the JVM generates three objects: "A", "B", and "AB. In addition, the performance of String concatenation is also very low. Therefore, stringbuffer and stringbuilder are usually used for string processing.
7. Selection of packages and Basic Types
In the code, if you can use the basic data type as the local variable type, try to use the basic data type, because the basic type variables are stored in the stack, and the packaging class variables are in the heap, stack operations are much faster than stack operations.
8. Assign the unused variable reference to null as soon as possible.
This can help JVM to recycle memory faster. Of course, many people do not catch a cold with this practice.
9. release resources in the finally Block
A typical scenario is that when I/O streams are used, the streaming should be closed in finally regardless of whether an exception occurs or not.
10. When using an object as the key in hashmap, pay attention to how to distinguish whether the object is the same
In the hashmap Implementation of JDK, the standard for determining whether the keys of two object types are the same is whether the hashcode is the same as the return value of the equals method. If you need to store two memory objects with the same data as different keys in hashmap, overwrite the hashcode and equals methods.