Common Java non-canonical code

Source: Internet
Author: User

1. Format source code

Ctrl + Shift + F – Format the source code.

Ctrl + SHIFT + o– Manage import statements and remove unused statements

In addition to performing these two functions manually, you can have eclipse automatically format the source code and automatically manage the import statement when saving the file. To do this, in Eclipse, go to Window-> Preferences-> Java-> Editor-> Save actions and enable perform the selected actions on S Ave, select Format Source code and organize imports.

2. Avoid multiple return statements (exit points) in the method

The following code is not recommended because it has more than one exit point (return statement):

Private Boolean iseligible (int age) {

if [age >] return

true; 

} else{return

false;

}

The above code can be written like this:

Private Boolean iseligible (int age) {

Boolean result = false;

if (age > A) result

= true; 

}

return result;

}

It can also be written as:
Private Boolean iseligible (int age) {return

age>18;

}
3. Do not create a new instance for Boolean,integer or string

Avoid creating new instances such as boolean,integer,string. Use Boolean.valueof (true) instead of new Boolean (true). The two kinds of writing effect are similar but can improve performance.

4. Name the public static final type member variable in uppercase:

5. If you need to use the same string in more than one place, create a string constant to use.

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.