Suggestions for writing good code in Java programming

Source: Internet
Author: User

Recently, I have been optimizing application performance and accumulated some rules and experience during the review code process. The goal of implementing these rules is to write "beautiful" code.

1. Make comments as comprehensive as possible

A Method annotation should contain detailed input parameters and result descriptions, and details should be given if an exception is thrown. A Class annotation should contain the class function descriptions, author, and modifier.

2. It is best to convert the same variable used for multiple times into a constant.

Variables with the same value used in multiple places should be summarized as a constant to facilitate future maintenance.

3. Execute method calls in the loop as few as possible

Try to avoid method calling in the loop, which can save the creation of the method stack. For example:

 
  1. for(int i=0;i<list.size();i++){  
  2.   System.out.println(i);  
  3. } 

It can be changed:

 
  1. for(int i=0,size=list.size();i<size;i++){  
  2.   System.out.println(i);  
  3. } 

4. Constant definitions can be placed in interfaces.

In Java, only constants are allowed in the interface. Therefore, you can save the public static final keywords by placing constants in the interface declaration.

5. Selection of arraylist and rule list

This problem is common. Generally, it is better for programmers to evaluate the use cases of the list and then make choices based on the features. The underlying layer of arraylist is implemented using arrays. Therefore, random data reading is much faster than sorted list, while sorted list is implemented using linked lists. Data addition and deletion are much faster than arraylist.

6. String, stringbuffer, and stringbuilder

This problem is also common. During String concatenation, strings usually generate multiple objects and cache multiple values in the constant pool. For example:

    String a="a";      String b="b";      a=a+b; 

In this case, the JVM generates three objects: "A", "B", and "AB. In addition, the performance of String concatenation is also very low. Therefore, stringbuffer and stringbuilder are usually used for string processing.

7. Selection of packages and Basic Types

In the code, if you can use the basic data type as the local variable type, try to use the basic data type, because the basic type variables are stored in the stack, and the packaging class variables are in the heap, stack operations are much faster than stack operations.

8. Assign the unused variable reference to null as soon as possible.

This can help JVM to recycle memory faster. Of course, many people do not catch a cold with this practice.

9. release resources in the finally Block

A typical scenario is that when I/O streams are used, the streaming should be closed in finally regardless of whether an exception occurs or not.

10. When using an object as the key in hashmap, pay attention to how to distinguish whether the object is the same

In the hashmap Implementation of JDK, the standard for determining whether the keys of two object types are the same is whether the hashcode is the same as the return value of the equals method. If you need to store two memory objects with the same data as different keys in hashmap, overwrite the hashcode and equals methods.

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.