The purpose of defining the specification is to unify the code style of the project so that the program is readable. All naming rules must adhere to the following rules:
In Java we generally use the Hungarian notation, the basic structure of scope_typevariablename, which uses a 1-3-character prefix to represent the data type, 3-character prefixes must be lowercase, the prefix is a word with a strong ideographic words or multiple words of the name, and the first letter of each word capitalized, the other letters lowercase, so that the variable name can be correctly segmented. For example, define an shaping variable that is used to record the number of documents: Intdoccount, where int indicates the type of data, followed by a ideographic English name, capitalized for each word. In this way, a variable name can reflect both the meaning of the variable type and the value stored by the variable, which makes the code statement more readable and easier to understand. byte, int, char, long, float, double, Boolean, and short.
1), the name can only be composed of letters, numbers, underscores, $ symbols
2), cannot start with a number
3), name cannot use keywords in java.
4), not to be allowed to appear in Chinese and pinyin naming.
Name of the project name (all lowercase) package (all lowercase, defined by the domain name)
The Java package name is made up of lowercase words. However, due to the Java object-oriented programming feature, each Java programmer can write their own Java package, in order to guarantee the uniqueness of each Java package naming, in the latest Java programming specification, the programmer is required to add a unique prefix to the name of the package they define. Because domain names on the Internet are not duplicated, programmers generally use their own domain name on the Internet as the only prefix for their packages.
Example: Net.frontfree.javagroup
Name of Class (the first letter of the class name is capitalized, if the class name consists of multiple words, the initials of each word are capitalized)
By convention, the Java class name usually begins with an uppercase letter, and if the class name consists of multiple words, the first letter of each word should be capitalized such as Testpage, and if the class name contains a word abbreviation, each letter of the word should be capitalized, such as: Xmlexample, One more naming technique is that because a class is designed to represent an object, you should choose a noun when you name the class.
Example: Graphics,myfirstclass
Name of the method (lowercase first letter, if the name consists of multiple words, the first letter of each word is capitalized)
The first word of the name of the method should begin with a lowercase letter, and the following words begin with a capital letter.
Example: DrawImage
Name of the variable
The main naming conventions are the following three types:
Camel notation: The first letter is lowercase and the next word starts with a capital letter
Pascal notation: The first letter is capitalized and the next word starts with a capital letter
Hungarian notation: Appends a lowercase sequence to a variable in Pascal notation to indicate the type of the variable
Name of the constant (all uppercase, often underlined)
The names of the constants should all use uppercase letters and indicate the full meaning of the constant. If a constant name consists of multiple words, you should use underscores to split the words.
Example: Max_value
Name of the parameter
The naming conventions for parameters are the same as the naming conventions for methods, and to avoid confusion when reading a program, make the name of the parameter as clear as possible if the parameter name is a single word.
Javadoc Comments (class comments)
java in addition to our common annotation approach, the Java language Specification defines a special kind of annotation, That is what we call the Javadoc annotation, which is used to record the API in our code. Javadoc comments are a multiline comment that begins with/** and ends with a comment that can contain some HTML tags and specific keywords. The advantage of using the Javadoc annotation is that comments written can be automatically converted to online documents, eliminating the hassle of writing program documents separately.
For example:
/**
*copyright (C), 2015-2016,xiangtanlovoinfo.co.,ltd.
*this is an example of
*filename:test.java
*javadoc
* @author jacksile
* @date 5/11/2015
* @version 0.1
*/
At the beginning of each program, generally use Javadoc annotation to the overall description of the program and copyright information, and then in the main program for each class, interface, method, field to add Javadoc comments, the beginning of each note in a sentence to summarize the class, interface, methods, fields completed functions, This sentence should occupy a single line to highlight its general role, in the following sentence can follow a more detailed description of the paragraph. After a descriptive paragraph, you can also follow some special paragraphs that start with the Javadoc comment tag, such as the @auther and @version in the example above, which appear in the generated document in a specific way.
Attribute Comment
You must precede each property with a property comment, and the comment template is as follows:
/* Prompt Message */
Privatestringstrmsg=null;
Method Comment
You must add a method comment before each method, and the comment template is as follows:
/**
* Detailed instructions for using the class method
* Instructions for use of @param parameter 1 parameter 1
* @return A description of the return result
* @throws exception type. The error code indicates the description of the exception thrown from such a method
*/
Construction method Comments
You must precede each construction method with a comment, with the following comment template:
/**
* Detailed usage instructions for the construction method
* Instructions for use of @param parameter 1 parameter 1
* @throws exception type. The error code indicates the description of the exception thrown from such a method
*/
Method Internal Comment
Use a single-line or multiline comment inside the method, which is added according to the actual situation.
Background color
Colorbgcolor=color.red
data type/prefix (attached)
Byteb
Charc
Shortsh
Inti
Longl
Charc
Strings
FLOATF
doubled
Hashtableh
[]arr
Listlst
Vectorv
STRINGBUFFERSB
BOOLEANB
bytebt
Mapmap
Objectob
For global variables to be used within multiple functions, add "g_" to the front. For example, a global string variable: G_struserinfo.
The following points should be noted when naming variables:
• Select a meaningful name and note that the first letter of each word should be capitalized.
• Do not use the same variable in a function to represent two values that have different meanings before and after.
• I, j, K, etc. are only used as cyclic index variables for small loops.
• Avoid naming state variables with flag.
• Use is to name logical variables, such as: Blnfileisfound. This gives the Boolean variable a positive form of naming, allowing other developers to understand more clearly what Boolean variables represent.
• If necessary, add a computed qualifier at the end of the variable, such as: Cursalessum.
• Naming does not include, Cursales and Cursalessum.
The name of the staticfinal variable (constant) should be capitalized and indicate the full meaning.
• If you need to abbreviate variable names, be sure to note the consistency of the abbreviation rules throughout your code. For example, if you use intcnt in some areas of your code, and you use intcount in other areas, you add unnecessary complexity to your code. It is recommended to avoid abbreviations in variable names.
• By placing a quantifier at the end, you can create more uniform variables that are easier to understand and easier to search. For example, use Strcustomerfirst and strcustomerlast instead of using Strfirstcustomer and Strlastcustomer. The commonly used quantifier suffixes are: first (one in a set of variables), last (the final of a set of variables), next (the next variable in a set of variables), Prev (the previous one in a set of variables), Cur (the current variable in a set of variables).
• Choose the best data type for each variable, which reduces the need for memory, speeds up code execution, and reduces the likelihood of errors. The data type used for the variable may affect the result of the variable being evaluated. In this case, the compiler does not produce a run-time error, it simply forces the value to conform to the data type requirements. This kind of problem is extremely difficult to find.
• Minimize the scope of variables. If the scope of the variable is greater than it should be, the variable can continue to exist, and the resource is still occupied for a long time after the variable is no longer needed. The main problem is that any method in any class can modify them, and it is difficult to keep track of where they were modified. Occupying resources is an important issue involved in the scope. Minimizing the scope of a variable can have a huge impact on the reliability of the application.
With regard to the method of naming constants, in Java code, it is recommended to apply constants instead of numbers and fixed strings whenever they are used. That is, the program should try to avoid other numbers except 0 and 1. Constants can be concentrated at the beginning of a program's definition or wider scope, the names should all use uppercase letters, and indicate the full meaning of the constant. If a constant name consists of multiple words, you should use the underscore "_" to divide the words such as: Num_days_in_week, Max_value.
Java Naming conventions