Java: Naming the Dainty
1. Class name
A class name usually ends with a noun. And in the class name to reflect whether it is to save data primarily or to provide the main function. For example Connectionbuilder This class we can all guess its main function is to create Connection objects,
The class name ending with the verb-er/or should contain at least one method that begins with the verb. For example Connectionbuilder This class, it should at least contain a method that starts with a build-. With this tacit understanding, others will be able to use this class more easily.
2, the name of the method of increasing the search and deletion
Taking the deletion of person class as an example--
The method for creating a person object is generally named Createperson ()/newperson (),
The Add method is named Addperson (), and the modification method is named Updateperson (),
The Delete method is named Deleteperson ()/removeperson (),
Named Getperson ()/queryperson () based on the method of the primary key query.
Query based on other criteria, indicating the specific condition type, such as Getpersonbyname () or getpersonbyage ().
The name of the method should be uniform. For example, when you start with deletexxx instead of removexxx, the whole project should be used in this way.
3. Counting method
The counting method is usually named Getnumberofxxx ()/getxxxcount (). For example Getnumberofrows (), Getquestioncount ().
4, the method of returning Boolean value
In JavaBean, the return Boolean attribute must be named with Isxxx ().
A method that checks whether a property is empty or if a record exists, usually named Hasxxx (), such as Hasresult ().
Methods that examine the state of an object are usually named after a is+ adjective. For example isclosed (), IsReady ()
5, return the collection object method
The method of returning the collection object should reflect the plural form, such as getpersons (), or the container type, such as getpersonlist ().
6, do not simplify the word
Do not write Gethislist (), but write Gethistorylist (), not write Usrno, but write Usernumber. A simple principle is to be able to read this name out.
7, the name of the parameter should be accurate
CreateUser (String str1, String str2) looks very difficult to understand, but if written as CreateUser (string userName, string password) is understood.
8, the return value is named result
Naming a return value in a method can make the process clearer.
9, interface
Interface naming does not have any special principle, and the same as class name to be able to accurately reflect the function of the interface.
Someone likes to add I in front of the interface name. It's perfectly OK. Regardless of the addition, we must ensure the unity of the whole project.
Java: Values and references for variables
Broadly speaking, in Java, you can refer to an object, or an identifier that contains a value, as a variable.
The contents of a variable may be a value, or it may be a reference to an object.
When the type of the variable is the base type (short, byte, int, long, float, double, Boolean, char), the content of the variable is a value; When the variable is of type object and its subclasses, the contents of the variable are references to an object. For example:
int a = 3;
String S1 = "hello!";
In this case, the content of variable A is 3, the content of variable S1 is to point to a reference to a string object, S2 and S1 point to the object. If the following statement is executed:
The contents of variable A are changed to 4,s1 content to point to another string object ("Hello again!" ) is referenced. S2 is still pointing to the original string ("Hello!" )。
Next talk about the method call. The call to the method is a value-passing. For example, the following method:
void f (int i, String s) {
i = m; The value of I is changed to
s = "hi!"; s now points to the string "hi!"
When F (A, S1) is invoked, the values of variables a and S1 are assigned to the parameters I and S, before the F method is executed. That is, no matter how I and S change, it does not affect the values of a and S1.
The next step is a slightly more complex example. First, define a method:
void f (int i, list list) {
i = ten;
List.add ("hello!");
Suppose you execute the following sentence again:
int a = 3;
List L = new ArrayList ();
So what happens after the execution?
I and a are two different variables, the value of I is changed not to affect a, so the value of a is 3;
The list and L are two different variables, but they point to the same list object. F is executed to add an element to this List object, then after execution, we can take this element through the L variable:
But if F writes like this:
void f (int i, list list) {
i = ten;
List = new ArrayList ();
List.add ("hello!");
So list first references to another list object, which changes the contents of another list object, so the list object that the variable L points to does not change, and l.size () is still 0.
A concept like this, beginners may be easily confused, but as long as the practice, will soon be familiar with.