General naming methods and values of variables and their references for Java primer

  • 2020-04-01 04:07:50
  • OfStack

  Java: nomenclature

1, the name of the class
Class names usually end in a noun. And in the name of the class, you want to show whether it is more about saving data or providing functionality. For example, ConnectionBuilder is a class whose main function we can guess is to create Connection objects,

A class name that ends with the verb -er/or should contain at least one method that begins with that verb. For example, the ConnectionBuilder class should contain at least one method that starts with build-. With this understanding, others can use the class more easily.

2, add, delete, change, check method of naming
Take the addition, deletion, and modification of the Person class for example --
The method to create a Person object is usually called createPerson()/newPerson(),
Add method named addPerson(), modify method named updatePerson(),
DeletePerson ()/removePerson(),
The method of querying by primary key is named getPerson()/queryPerson().
Depending on other criteria, the query indicates the specific condition type, such as getPersonByName() or getPersonByAge().

The name of the method should be uniform. For example, if you started with deleteXXX instead of removeXXX, then this is how the entire project should be used.

3. Counting method
The counting method is usually named getNumberOfXXX()/getXXXCount(). For example, getNumberOfRows(), getQuestionCount().

Method that returns a Boolean value
In javabeans, the return Boolean property must be named with isXXX().

A method to check whether a property is empty or whether a record exists is usually named hasXXX(), such as hasResult().

Methods to check the state of an object are usually named after the is+ adjective. For example, isClosed(), isReady()

5, return the collection object method
Methods that return collection objects should be plural, such as getPersons(), or container types, such as getPersonList().

6. Don't simplify words
Instead of getHisList(), write getHistoryList(); Instead of writing usrno, write userNumber. A simple rule is to be able to pronounce the name.

7. Parameter naming should be accurate
CreateUser (String str1, String str2) might seem a little hard to read, but it's easier to write createUser(String userName, String password).

8. The return value is named result
Naming the return value result in a method makes the thread of the method clearer.

9, interfaces,
There is no special rule for interface naming, just like the class name, it should accurately reflect the function of the interface.
 
Some people like to prefix interface names with I. Absolutely. Make sure the project is consistent whether you add it or not.


Java: values and references for variables
Broadly speaking, in Java, any identifier that can point to an object or contain a value can be called a variable.

The contents of a variable can be either a value or a reference to an object.

When the type of the variable is the basic type (short, byte, int, long, float, double, Boolean, char), the content of the variable is a value; When a variable is of type Object and its subclasses, its content is a reference to an Object. Such as:


int a = 3; 
String s1 = "Hello!"; 
String s2 = s1; 

In this case, the content of variable a is 3, the content of variable s1 is a reference to a string object, and s2 is a reference to the object just like s1. If the following statement is executed:


a = 4; 
s1 = "Hello again!"; 

The contents of variable a are changed to 4, and the contents of s1 are changed to point to another string object ("Hello again!"). ). S2 is still pointing to the same string ("Hello!"). ).

Let's talk about method calls. Method calls are passed. For example:


void f(int i, String s) { 
  i = 100;   //The value of I is changed to 100
  s = "Hi!";  //S now points to the string "Hi!"
} 

When f(a, s1) is called, the values of the variables a and s1 are assigned to the parameters I and s before the f method is executed. In other words, whatever happens to I and s, it doesn't affect the values of a and s1.

Here's a slightly more complicated example. First, define a method:


void f(int i, List list) { 
  i = 10; 
  list.add("Hello!"); 
} 

Suppose you execute the following sentence again:


int a = 3; 
List l = new ArrayList(); 
f(a, l); 

So what happens after execution?
I and a are two different variables, and it doesn't affect a if the value of I changes, so the value of a is still 3;
List and l are two different variables, but they point to the same list object. During the execution of f, an element is added to the List object. After the execution, we can get this element through the l variable:


l.get(0); // "Hello!" 

But if f is written like this:


void f(int i, List list) { 
  i = 10; 
  list = new ArrayList(); 
  list.add("Hello!"); 
} 

So the list first refers to another list object, and what has changed is the contents of the other list object, so the list object pointed to by the variable l has not changed, and l.s-ize () is still 0.

Concepts like these can be confusing to beginners; But with practice, you'll soon get used to it.



Related articles: