Java Code optimization Strategy

Source: Internet
Author: User

1. How to use exception

Exception reduces performance. An exception throws the first need to create a new object. The constructor in the Throwable interface calls the local method named Fillinstacktrace (). This method is responsible for the entire frame of the patrol stack to collect tracking information. So whenever an exception is thrown, it requires the virtual machine to mount the call stack because a new object is created in the middle.

Exceptions should be used only when there is an error, not the flow.

2. Do not initialize variables two times

Java initializes the variable to a known value by invoking the unique class constructor. All objects are set to null, integers (byte, short, int, long) are set to 0,float and double set to 0.0,boolean variable set to False. This is especially important for classes that extend from other classes, which are called automatically by all a series of constructors when an object is created with a new keyword.

3. Let the class be final in any possible place

A class that is marked final cannot be extended. There are a number of examples of this technology in the core Java API, such as java.lang.String. Marking the string class as final prevents developers from creating a method of length that they implement themselves.

More to the point, if the class is final, the method of all classes is final. The Java compiler may inline all of the methods (this relies on the implementation of the compiler). In my tests, I've seen an average performance increase of 50%.

4. Use local variables wherever possible

The arguments that are part of the method invocation and declare temporary variables that are part of the call are stored on the stack, which is faster. such as static, instance (instance) variables, and new objects are created in the heap, which is relatively slow. The more in-depth optimization of local variables depends on the compiler or virtual machine you are using.

5. Stop Wit

Many developers write reusable and flexible code in their brains, and sometimes incur extra overhead in their programs. Once or another, they wrote code like this:

public void doSomething(File file) {
FileInputStream fileIn = new FileInputStream(file);
// do something

He is flexible enough, but at the same time they have more overhead. The thing behind this idea is to manipulate a inputstream instead of a file, so it should be rewritten as follows:

public void doSomething(InputStream inputStream){
// do something

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.