Java Naming Rules and Specifications

Xiaochat : When you first started learning, would there be anyone like me who was hesitant about naming some Java file names and variable names? What are the rules and specifications for naming? What name is more convenient?
In fact, after studying for a long time, everyone will naturally become proficient, so don't worry too much if you can't remember it for a while. As for me, Xiaobai, here is a brief summary, hoping to help you who are not familiar with it at the beginning.

1. Java naming rules (must be followed)

The identifier needs to be named according to the restrictions of Java, otherwise an error will be reported. (What is an identifier? There is an explanation below to help you recall.【Click here to have a look】 )

  • Identifiers can contain 26 English letters (case-sensitive), 0-9 numbers, $ (dollar sign) and _ (underscore).
  • Identifiers cannot start with a number.
  • Identifiers cannot be keywords.

1.1. Examples

  • Examples of valid identifiers:
    insert image description here

  • Examples of invalid identifiers:
    insert image description here


2. Java naming convention (soft suggestion)

  • Class and interface names: the first letter is capitalized, and the first letter of each subsequent word is capitalized (upper camel case).

    public class Fruit(){
           
           }
    public interface FruitFactory(){
           
           }
    
  • Method name: The first letter is lowercase, and the first letter of each subsequent word is capitalized (small camel case).

    public class Action() {
           
           
        // 方法
        public void eat() {
           
           }
        public void goHome() {
           
           }
    }
    
  • Variable names: All lowercase as far as possible, and compound words can be named in camel case.

    public class People() {
           
           
    	int id;
        String username;
        String password;
        Double saveMoney;
    }
    
  • Constant name: all capital letters, use underscore _ to separate words.

    public class Summer() {
           
           
        private final String[] PLAYS = {
           
           "swim", "fishing", "eat an ice cream"};
        private final int MAX_TEMPERATURE = 40;
        private final int MIN_TEMPERATURE = 36;
    }
    
  • Package name: each in all lowercase

    pers.xiaobai.xxxproject.xxxmodule
    com.tenxun.qq.xxxmodule
    

3. Suggestions for naming habits (see personal habits)

The name of the identifier should be as clear as possible. Except when doing ACM or testing by yourself, it is generally taboo to use unknown names such as a, b, and c in the project. If the name is too long, you can take the abbreviation of some words as part of the compound variable name, such as the student management system: stuManagementSys.

Complying with the naming convention is to better understand other people's or even your own code. In the future, you will definitely add new functions to other people's code or solve the code bugs you wrote a long time ago. At that time, you will be able to experience the standard Good thing.

idea has a code specification plug-in from Alibaba, which is used to prompt some programming specifications. It is recommended to use it at the beginning, and maintain good programming habits earlier.
insert image description here


4. Expand

4.1. Naming rules for Java package names

  • personal

    • indi: completed by multiple people, the copyright belongs to the originator

      包名为:indi.发起者名.项目名.模块名
      
    • pers: Completed alone, public, copyright mainly belongs to individuals

      包名为:pers.个人名.项目名.模块名
      
    • priv : Completed alone, non-public, copyright belongs to the individual

      包名为priv.个人名.项目名.模块名
      
  • team:

    • team: A team project refers to a project initiated and developed by a team, and the copyright belongs to the team.

      包名为team.团队名.项目名.模块名
      
  • company:

    • com: Initiated by the company, the copyright is owned by the company that initiated the project.

      包名为com.公司名.项目名.模块名
      

Original link: https://blog.csdn.net/qq_38534524/article/details/89441607


4.2. Identifiers

In the Java language, for the names of variables, constants, functions, and statement blocks, we collectively call them Java identifiers.

Identifiers are used to name classes, objects, methods, variables, interfaces, and custom data types .

To put it bluntly, it is to give the element a name as an identifier to distinguish it.

Click here to jump back



essay

insert image description here

Guess you like

Origin blog.csdn.net/m0_48489737/article/details/121443471