Java performance optimization reading notes (1)
1. Create objects by clone ()
All classes in the Java language inherit from Java by default Lang. object, there is a clone () method in this class:
The copied object returns a new object, not the reference address of an object;
The difference between the copied object and the new object returned by the new keyword operation is that the copy already contains some information of the original object rather than the initial information of the object.
Implementation of factory mode:
Counterexample
Positive example
Use clone() method
2. Avoid Boolean judgment
Boolean one byte 8 bits
Counterexample
Positive example
3. Multipurpose conditional operator
Counterexample:
Positive example:
4. Static method instead of example method
Tool class
5. Conditional use of final field
Final tells the compiler that the method will not be overloaded
6. Avoid unnecessary instanceof operations
7. Avoid parent class conversion in subclasses
Object is the parent of all classes
8. It is recommended to use more local variables
9. The most efficient way of algorithm -- bit operation
10. Replace two-dimensional array with one-dimensional array
The access speed of two-dimensional array is better than that of one-dimensional array, but the memory occupied by two-dimensional array is greater than that of one-dimensional array.
When memory is sensitive, you can convert the processing
11. Boolean operation instead of bit operation
&& ||
12. Extract expression optimization
13. Don't always use the reverse operation fee!
14. Do not initialize variables repeatedly
15. Thinking about variable initialization process
16. Object creation and access process
17. Use string in switch statement
18. Improvement of numeric literal quantity
0b, 0b and_
19. Method call for optimizing variable length parameters
@SafeVarargs
20. Optimization for basic data types
Integer. compare(x,y)
21. Null variable