Java code optimization skills that developers should master __java

Source: Internet
Author: User

Just like a whale eats shrimp, maybe a two-shelled shrimp is not very useful for a whale, but a lot of shrimp is eaten and the whale is naturally full.


Code optimization, perhaps a two optimization, to improve the efficiency of the code is not significant, but as long as everywhere can pay attention to code optimization, in general, to improve the efficiency of the operation of the code is very useful.


This view, in the present, is a reason for code optimization, but not all right. In the development of mechanical technology today, the server 8 cores, 16 cores, 64-bit CPU, code execution efficiency is very high, StringBuilder replacement StringBuffer, ArrayList replacement vector, for the code to improve the efficiency of the operation is negligible, Even if every point in the project is noticed, the code does not see any noticeable change in its operation.


I think the most important role of code optimization should be: avoid unknown errors.


In the process of running the code line, there are often many unexpected errors, because the online environment and development environment is very different, wrong positioning to the end is often a very small reason. However, in order to solve this problem, we need to validate, then package out the class file to be replaced, suspend the business and restart, for a mature project, the final effect is very large, which means that the user can not access the application in this period.


Therefore, when writing code, from the source to pay attention to a variety of details, weighing and using the best choice, will largely avoid the unknown error, in the long run greatly reduce the workload.


The goal of code optimization is:

Reduce the size of your code

Improve the efficiency of code running


The content of this article comes from the network, some from the normal work and learning, of course, it is not important, it is important that the details of the code optimization is really useful. This article will maintain a long-term update, as long as there is a need to share the code optimization details, it will update this article.


1. Specify the final modifier for the class and method as much as possible


A class with the final modifier is not derived. In the Java Core API, there are many examples of final application, such as java.lang.String, where the entire class is final. Specifying the final modifier for a class allows a class not to be inherited, and specifying the final modifier for a method can allow the method to be overridden.


If you specify that a class is final, all methods of that class are final. The Java compiler looks for opportunities to inline all final methods, and inline is important for improving the efficiency of Java operations, as detailed in Java runtime optimizations. This will increase performance by an average of 50%.


2. Reuse objects as much as possible


In particular, the use of String objects should be replaced with stringbuilder/stringbuffer when strings are concatenated. Because Java virtual machines not only take time to generate objects, they may also take time to garbage collect and process these objects, so generating too many objects will have a significant impact on the performance of the program.


3. Use local variables whenever possible


The parameters passed when the method is invoked and the temporary variables created in the call are saved on the stack, and the other variables, such as static variables, instance variables, and so on, are created in the heap at a slower rate.


In addition, the variables created in the stack, as the method is run over, are gone and no additional garbage collection is required.


4. Close the flow in time


Java programming process, the database connection, I/O flow operation must be careful, after the use, timely shutdown to release resources. Because the operation of these large objects will cause a large system overhead, a slight carelessness, will lead to serious consequences.


5. To minimize the duplication of the calculation of variables


Clear a concept, the call to the method, even if only one sentence in the method, is also consumed, including the creation of stack frames, call methods to protect the scene, when the method is completed to restore the scene. So for example, the following actions:

   
   
    
    

for (int i = 0; i < list.size (); i++)

{...}


Suggested replacements are:

   
   
    
    

for (int i = 0, length = list.size (); i < length; i++)

{...}


In this way, when the list.size () is very large, it reduces a lot of consumption.


6. Try to use lazy load strategy, that is, when needed to create


For example:

 

String str = "AAA";

if (i = = 1) {

List.add (str);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.