Java Performance Optimization

Source: Internet
Author: User
Tags string methods

1. Use exceptions with caution.

Exceptions are detrimental to performance. To throw an exception, you must first create a new object. The constructor of the throwable interface calls the local (native) method named fillinstacktrace (), the fillinstacktrace () method checks the stack, and collects Call trace information. As long as an exception is thrown, the VM must adjust the call stack because a new object is created during processing. Exceptions can only be used for error handling and should not be used to control program processes.

2. Do not reinitialize the variable.

By default, Java initializes the variable to a definite value when calling the class constructor: all objects are set to null, and integer variables (byte, short, Int, long) set it to 0, float and double variables to 0.0, and logical value to false. This is especially important when a class is derived from another class, because when an object is created using the new keyword, all constructors in the constructor chain are automatically called.

3. Use local variables whenever possible.

The parameters passed during method calling and the temporary variables created in the call are saved in the stack, which is faster. Other variables, such as static variables and instance variables, are created in heap, which is slow. In addition, local variables may be further optimized depending on the specific compiler/JVM.

4. Specify the final modifier of the class as much as possible.

Classes with final modifiers cannot be derived. There are many examples of final applications in the Java core API, such as Java. Lang. String. Specifying final for the string class prevents people from overwriting the length () method. In addition, if you specify a class as final, all the methods of this class are final. This can increase the performance by an average of 50%.

5. Create a class instance without the new keyword.

When you create an instance with 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 constructor.

When using design pattern, it is very easy to use the clone () method to create a new object instance if you create an object in factory mode.

6. Use the string connection operator + with caution.

The String concatenation operator + seems simple, but it actually consumes a lot of system resources. The compiler can efficiently connect strings, but variable strings require considerable processor time. For example, suppose S and T are string variables: system. Out. println ("heading" + S + "trailer" + t );
The preceding statement requires a new stringbuffer (string buffer), append the independent variable, and use tostring () to convert the result back to a string. Therefore, disk space and processor time are greatly consumed. If you want to append multiple strings, you can use a string buffer directly-especially when you can reuse it in a loop. By prohibiting the creation of a new string buffer in each loop, you can save 980 units of object creation time (as described above ). Using substring () and other string methods can further improve the performance. If feasible, the character array speed can be even faster.

7. Use the synchronization method with caution.

In the JDK interpreter, calling a synchronous method is usually 10 times slower than calling a non-synchronous method. So try to avoid using the synchronization method-if not, the method synchronization is faster than the code block synchronization.

8. Try to use Java API classes.

Try to use classes from Java APIs because they have been optimized for machine performance. This is hard to achieve with Java. For example, when copying an array of any length, arrarycopy () is much faster than loop.

 

 

Related Article

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.