Java Identifiers and Naming Principles

1. Java identifier:

In Java, an identifier is a name used to name program elements such as variables, methods, classes, packages, etc. Identifiers must follow some rules and conventions to ensure correctness and consistency in a programming language. Following are the rules for Java identifiers:

  1. Can consist of letters, numbers, underscores (_), and dollar signs ($).
  2. Must start with a letter, underscore, or dollar sign, not a number.
  3. Case sensitivity: "myVariable" and "myvariable" are treated as two different identifiers.
  4. Keywords cannot be used as identifiers. For example, the keywords if, for, while, etc. cannot be used as variable names or method names.
  5. Identifiers cannot contain spaces or other special characters such as @, #, %, etc.
  6. Java identifiers should be descriptive and follow the CamelCase naming convention (CamelCase) or use underscores to separate. For example, myVariableName or my_variable_name.

Examples of legal Java identifiers:

int myVariable;
String userName;
void calculateTotalAmount();
double average_score;

 Examples of illegal Java identifiers:

int 123abc; // 以数字开头
double my-var; // 包含特殊字符“-”
String if; // 使用了关键字作为标识符

Second, the naming principles commonly used in Java: 

  1. Class names: Use nouns or noun phrases to name class names, with the first letter of each word capitalized (UpperCamelCase).
    For example: Person, StudentInfo, Car.
  2. Interface name: use adjectives or adjective phrases to name interface names, capitalize the first letter of each word (CamelCase).
    For example: Runnable, Serializable, Comparable.
  3. Method names: use verbs or verb phrases to name method names, the first letter of the first word is lowercase, and the first letter of subsequent words is capitalized (small camel case).
    For example: calculateTotal, printData, getUserInfo.
  4. Variable names: Use descriptive nouns or noun phrases for variable names, with the first letter of the first word lowercased and subsequent words capitalized (CamelCase).
    For example: age, firstName, totalAmount.
  5. Constant name: Use all uppercase letters and underscores to represent constants. If there are multiple words, separate them with underscores.
    For example: MAX_LENGTH, PI, DEFAULT_TIMEOUT.
  6. Package name: Use all lowercase letters, and use dots (.) to separate multiple words.
    For example: com.example.project.
  7. Enumerated type names: Named in the same way as class names, using nouns or noun phrases, with the first letter of each word capitalized.
  8. Enumeration constant name: Use all uppercase letters and underscores to represent enumeration constants, and separate multiple words with underscores.
  9. Local variable names: Use descriptive nouns or noun phrases with the first letter lowercased and subsequent words capitalized (lower camelCase).
    For example: count, userName, totalAmount.

Following these naming guidelines can make your Java code clearer, easier to read, and easier to maintain. Also, be careful to avoid using identifiers that are the same as Java keywords, and try to avoid using ambiguous or meaningless names. 

Guess you like

Origin blog.csdn.net/weixin_44523517/article/details/131897375