Tips for using Java classes and objects

Source: Internet
Author: User

Just saw an online on the Java performance tuning of the post, is very helpful, I also used more than 10 years of Java, some empathy, here, citing the post combined with their feelings to talk about

Tip 1: Use new to create objects as little as possible

Because when you create an instance of an object with new, all constructors on the constructor chain are called in turn, resulting in low efficiency. In fact, we can use some small techniques, such as reusing existing objects. For example, if a class implements the Cloneable interface, it can invoke the Clone () method of the object of the class to create a new object that does not invoke the constructor of any class. So it's faster to generate new objects than to call any of the classes ' constructors.

Tip 2: Use local variables as much as possible

If you learn the principles of the Java Virtual machine, you know that when you invoke a method, the parameters passed and the temporary variables created when the call is made are saved on the stack (stack), which is created faster. Static variables, instance variables, are created on the heap (heap), and this variable is created at a slow rate.

Tip 3: Minimize method calls

Object-oriented commission as far as possible with the method to direct access to the variable, rather than want to directly access the variable, but the method call will have a stack and other operations, which will affect the efficiency of the method. For example, using a setter and getter to access a variable is much less efficient than directly accessing a variable, but often using a method to access a variable is more readable, so we should weigh the readability and efficiency

Tip 4: Use static,final,private as much as possible

Because if the class level is modified to final, then the class is not inheritable, and all the methods in the class cannot be inherited. Then the compiler optimizes, and the method is inline to improve efficiency. At the method level, if a method is decorated to static,final,private, it is not overwritten, so Java has no need for dynamic association, which improves efficiency

Tip 5: Let Setter/getter also be final

Again, the purpose of this is to let the compiler know that this method is not overwritten, and then turn it into inline to improve efficiency.

Tip 6: Do not unnecessarily transform the class

For example, a is the parent of B, so there is no need for transformation from B to a.

Tip 7: Reuse string objects as much as possible

For example, to connect strings, you can use Stringbuffer/stringbuilder instead of string, because string objects connect, operations produce a large number of new string objects, and the system not only takes time to generate these objects, Finally, these newly generated objects are garbage collected and scavenged.

Tip 8: Don't create too much

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.