Java code optimization is Java programming development is very important one step, the Java code optimization optimization should pay attention to detail, a two details of the optimization, the effect is not big, but if everywhere: to optimize the code, the code to reduce volume, improve the efficiency of code to run there is a huge help, also can to some extent avoid unknown error, Common Java code optimization details are as follows:

1. Specify final modifiers for classes and methods

Specifying a final modifier for a class makes it impossible for a class to inherit, and specifying a final modifier for a method makes it impossible for a method to be overridden. If you specify a class as final, then all of its methods are final, and the Java compiler looks for opportunities to inline all final methods. Inlining makes a big difference in Java running efficiency, increasing performance by an average of 50%.

2. Reuse objects as much as possible

Because Java virtual machines spend time not only generating objects, but also garbage collecting and processing them, generating too many objects can have a significant impact on program performance.

3. Use local variables whenever possible

Parameters passed when calling a method and temporary variables created during the call are stored in the stack, which is faster, while other variables are created in the heap, which is slower, and variables created in the stack are lost as the method ends, without additional garbage collection.

4. Shut down the stream in time

During Java programming, be careful when you connect to the database and perform I/O flow operations. After using these large objects, close them in time to release resources. Operations on these large objects may cause heavy overhead.

5. Minimize the double calculation of variables

In a method call, even if the method is only a single statement, it is expensive, so you can minimize the repetition of variable definitions and references when making a method call.

6. Try to use a lazy loading strategy, that is, create only when you need to

7. Use exceptions with caution

Exceptions are bad for performance because the Java virtual machine must adjust the call stack whenever an exception is thrown, and exceptions should only be used for error handling, not to control program flow.

8. Do not use try… The catch… You should put it in the outermost layer

9. If you can estimate the length of the content to be added, specify the initial length for the underlying array implementation of the collection, utility class

10. To copy a large amount of data, run the system.arraycopy () command

11. Multiplication and division use shift operations

Using a shift operation can greatly improve performance, because at the bottom of the computer, counterpoint operation is the most convenient and fastest.

12. Do not keep creating object references within the loop

13. For efficiency and type checking purposes, use arrays whenever possible, and use ArrayList only when you cannot determine the size of the array.

14. Use HashMap, ArrayList, and StringBuilder wherever possible. Hashtable, Vector, and StringBuffer are not recommended unless thread-safe because they incur performance overhead due to synchronization

15. Do not declare arrays as public static final

Declaring an array public is a security loophole, which means that the array can be changed by an external class.

16. Use singletons when appropriate

In the case of controlling the use of resources, the generation of control instances and the sharing of control data, singletons can be used to reduce the load burden, shorten the load time and improve the load efficiency.

17. Avoid arbitrary use of static variables

18. Remove unnecessary sessions in a timely manner

19. Collections that implement the RandomAccess interface, such as ArrayList, should be traversed using the most common for loop rather than the foreach loop

20. Use synchronized code blocks instead of synchronized methods

Unless it is clear that an entire method needs to be synchronized, try to use synchronized code blocks to avoid synchronizing code that does not need to be synchronized, affecting the efficiency of code execution.

21. Declare the constant static final and name it in uppercase

22. Don’t create objects that you don’t use and don’t import classes that you don’t use

23. Avoid using reflection during program execution

Reflection is powerful, but inefficient. It is not recommended to use reflection frequently during program execution. If it is necessary, it is recommended to use reflection to instantiate an object and put it into memory at the start of classes and projects that need to be loaded by reflection

24. Use database connection pools and thread pools

Both pools are designed to reuse objects, the former to avoid frequent opening and closing of connections, and the latter to avoid frequent thread creation and destruction.

25. Use buffered I/O streams for IO operations

26. ArrayList is used for scenarios with high sequential insertion and random access, and LinkedList is used for scenarios with high element deletion and intermediate insertion

27. Don’t have too many parameters in a public method

28. String variables and string constants equals before string constants

29. There is no difference between if (I == 1) and if (1 == I) in Java

30. Do not use the toString() method on arrays

31. Do not force downcasts of primitive data types that are out of scope

32. Remove unused data from public collection classes

ToString () is the fastest method, followed by String.valueof (data), and data + “is the slowest

34. Use the most efficient way to traverse the Map

35. It is recommended to separate close() for resources

36. Remove ThreadLocal before or after use

37. Remember to use constant definitions instead of devil numbers. Devil numbers can greatly reduce code readability

38. The initial assignment of long or long is to use a capital L instead of a lowercase L, because the letter L can easily be confused with the number 1. This point is worth noting in detail

39. All overridden methods must retain the @override annotation

40. It is recommended to use the new Objects utility class in JDK7 to compare Objects to equals(b) directly, with the risk of null pointer exceptions

41. Instead of using “+” for string concatenation in the loop, use StringBuilder to append the string

42. Do not catch run-time exception classes that inherit from RuntimeException defined in the Java class library

43. Prevent Random instances from being used by multiple threads. Although sharing Random instances is thread-safe, performance degrades due to competing with the same seed

44. Static, singleton, and factory classes make their constructors private

The above is commonly used Java code optimization method, in Java code writing to develop the habit of code optimization, can write small, high efficiency, low error rate of the code!