Java Naming rules: focus on details
Any language is used for communication and has its own writing specifications. Java is no exception. There are several advantages to paying attention to these details: 1. Good naming rules can make the written programs easier to be understood and maintained by others. Of course, it is easy to understand and expand in the future. At the same time, it also makes the program more standardized and professional. In today's humane world, our Java Naming rules should also focus on experience.
2. Understand naming conventions to better learn and remember classes and functions in Java class libraries.
3. Many naming rules involve English, which can promote English learning. For example, numbermax, maxnumber, and maxnumber are different. Maxnumber may be an attribute name; maxnumber is generally a class name; numbermax is more inclined to be a method. There is also a question about English collocation. For example: fileread and filereader. Obviously, the latter is better as the class name. Readfile and fileread, which is obviously more suitable as a method.
The following principles must be followed when writing Java programs:
If there is no good reason, never violate him.
1. Package
It consists of lowercase letters and a few numbers.
Java packages start with Java. And javax, for example, java. AWT.
Packages developed by other organizations start with the Internet domain name of the organization, for example, Com. sun, Com. borland example: COM. aliasi. hmm; COM. aliasi. classify; COM. aliasi. cluster; COM: Organization; aliasi: specific organization name or individual; classify, cluter: package name (topic) Org. apache. lucene; Org. apache. lucene. analysis. standard; Org. apache. nutch. NDfS; Org. apache. nutch. FS; Org. apache. nutch. DB; Org. apache. nutch. parse;
2. Class and Interface
It is composed of one or more words. The first letter of each word is capitalized, for example, stringbuffer.
Class: names are generally used as nouns and phrases; indexoptimizer. Java; webdbreader. Java
Interface: similar to the class, you can use adjectives, such as runnable and comparable.
3. Method
The naming rules of the class and interface are the same except the first letter in lower case. For example, getpersoninfo ()
For how to get and set attribute values:
Whether bean or not, follow the naming rules of JavaBean: getxxx (), setxxx ()
The conversion Object Type Returns different types of methods:
Name it totype, for example, tostring (), toarray ()
Method for returning a view:
Name it in the form of astype (), aslist ()
Return the method of the original type with the same value as the object that calls this method (Wrapper class:
Name it in typevalue () format, such as intvalue (), floatvalue ()
Parsecharacterencoding (string );
4. domain (attribute)
Common domain:
The naming rules of the class and interface are the same except the first letter in lower case. For example, personinfo
Constant number field:
It is composed of one or more words separated by underscores, such as values and negative_intinity.
The constant field is the only case where underlines are allowed.
5. Local Variables
The name is the same as the domain name, which can be abbreviated as "I", "J", "Temp", or "maxnumber ".
Sort data.