Java Development Specification Excerpt

Source: Internet
Author: User

For canonical JAVA derived classes, use the Eclipse tool to generate file formats and avoid using handwritten header files/implementation files.

Try to avoid a line that is longer than 200 characters because many terminals and tools cannot handle it well.
Indent 8 Cells

, the standard package name in import is preceded by the local package name and is arranged alphabetically.

If the import row contains a different subdirectory in the same package, it should be handled with *.

For constructors, it should be written in an incremented manner, and the constructor of the argument will be written in the following way

/*-
There should be a blank line before a single-line comment.
However, it can be used to comment out a contiguous number of lines of code.

/**
* Statement:? 2013-2014 All rights reserved
* Official website:http://www.****.com
* Copyright:? ********** All rights Reserved.
* @version: 1.0
* @author: Architect.bian
* Create at:2013-7-28 4:47:30
*/

/*
* Description Information of class
* @author
*/
public class string{

}


2. Constants in the exclusive constant class are all lowercase, easy to read and configure

Arrays should always be defined in the following way: byte[] Buffer

That is, for the traversal of some sets, use the enhanced for loop as much as possible instead of some variables (i).

A single declaration is recommended because it facilitates writing comments. i.e.

Try to initialize while declaring the local variable. The only reason for not doing this is that the initial value of the variable depends on some previously occurring calculation.

if (condition) {
statements;
} else if (condition) {
statements;
} else{
statements;
}

If not omitted {}

for (initialization; condition; update);

1. If there is insufficient justification, do not declare the instance or class variable as public. Typically, an instance variable does not need to be set explicitly (set) and gets
(get), which is usually generated as the edge effect of a method call (side effect).
2. A proper example of a variable with a public instance is that the class is only a data structure and has no behavior. i.e., if you want to use a structure
(struct) rather than a class (if Java supports structs), it is appropriate to declare the instance variable of the class as public.

Classmethod (); That's right
Aclass.classmethod (); That's right
Anobject.classmethod (); Error!

8.3 Constants ¶
The numeric constants that are used as counter values in the For loop, except for -1,0 and 1, should not be written directly to the code.

Do not use the inline (embedded) assignment operator to try to improve the efficiency of the runtime, which is the work of the compiler.

Try to make your program structure conform to the purpose. For example:


if (booleanexpression) {
return true;
} else {
return false;
}

This should be replaced by the following:
return booleanexpression;
In a similar way:
if (condition) {
return x;
}
return y;

should be written:
return (condition x:y);

1.for loop nesting should not be too much, up to three layers, or easily cause code confusion, not easy to read, but also prone to unexpected bugs

2. Use StringBuilder instead of string for a large number of string operations to improve efficiency and, if thread safety is required, use
StringBuffer;
3. For list operations, if there are a large number of update operations, use LinkedList; If the update operation is less, the majority of the query operation
, the ArrayList is used;
4. The number of rows per method should not be too many, otherwise it is likely to make the design of the method is unreasonable, should consider the reconstruction;
5. If a large number of if statements appear, try using a switch statement instead;
6. In a specific scope, if an object must be purged (not handled by the garbage collection mechanism), use the following method: Initialize the object
If it succeeds, it immediately enters a try block containing a finally clause and begins to clear the work;

Java Development Specification Excerpt

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.