The beauty of Java programming

Source: Internet
Author: User

How the Java programmer writes "Graceful" code, the Power node tells you what to do:

1. Comments as comprehensive as possible

The comments for the method should contain detailed entry and result descriptions, as well as details of the exception thrown, and the class's comments should contain the function description, author, and modifier of the class.

2, multiple use of the same variable is best summed up as a constant

Variables of the same value used in multiple places should be summed up as a constant to facilitate future maintenance.

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

Try to avoid making a few avoidable method calls in the loop so that you can save the creation of the method stack. For example:

1.for (int i=0;i<list.size (); i++) {<= "" p= "" >

2. SYSTEM.OUT.PRINTLN (i);

3.}

Can be modified to:

1.for (int i=0,size=list.size (); i<size;i++) {<= "" p= "" >

2. SYSTEM.OUT.PRINTLN (i);

3.}

4, the definition of constants can be placed in the connector

In Java, only constants are allowed in the interface, so it is possible to omit the publicstaticfinal by putting the constants in the interface declaration.

5. Selection of ArrayList and LinkedList

This problem is more common. It is often best for programmers to evaluate the use of a list and then make a choice based on the feature. ArrayList is implemented using arrays, so random reading of data is much faster than LinkedList, and LinkedList is implemented using a linked list, adding and deleting data faster than ArrayList.

6, String,stringbuffer and StringBuilder

This problem is also more common. When string concatenation is performed, a string typically produces multiple objects and caches multiple values in a constant pool. For example:

1.String a= "a";

2.String b= "B";

3.a=a+b;

In this case, the JVM produces "a", "B", "AB" three objects. And the performance of string concatenation is also very low. Therefore, it is often necessary to do string processing when using StringBuffer and StringBuilder as much as possible.

7, packaging and basic types of choice

In code, if you can use the base data type to do the local variable type, use the base data type as much as possible, because the variables of the underlying type are stored in the stack, and the variables of the wrapper class are in the heap, and the stack operates much faster than the heap.

8. Assign null to a variable reference that is no longer used as soon as possible

Doing so can help the JVM to recycle memory more quickly. Of course, many people do not really have a cold on this practice.

9. Releasing resources in the finally block

A typical scenario is when an IO stream is used, regardless of whether an exception occurs and finally the convection should be closed in finally.

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

In the HashMap implementation of the JDK, the criteria for determining whether a key of two type object is the same is whether the hashcode is the same and the return value of the Equals method. The hashcode and Equals methods are overwritten if the business needs to store two data objects of the same memory as different keys in HashMap.

The beauty of Java programming

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.