Textbook series variable (V) - Java naming conventions

Textbook series variable (V) - Java naming conventions

More, click here to learn, sign up for

Variable names must meet a series of conditions, can not be named
Step 1: Naming
Step 2: Use the full name of the word, not the abbreviation
Step 3: You can not use only keywords, but can include keywords
Step 4: Keyword list 1
step 5: keyword list 2
step 6: Chinese also be used to name variables

Example 1: Naming
variable name only alphanumeric $ _
variable is the first character only letters $ _
variable first character can not use the digital
Note: _ underscore, not - or minus - dash

int a= 5;
int a_12= 5;
int $a43= 5;
 
int a434= 5;
 
//第一个是数字,是不行的
int 34a= 5;

Example 2: Using complete word name, not the abbreviation
naming, try to use the full name for the word, such as name, moveSpeed, instead of using the abbreviations n, m.

Hero.java
Hero.java
public class Hero {
     //使用完整单词命名,易于理解
    String name;
      
    float hp;
      
    float armor;
      
    int moveSpeed;
}
public class Hero {
    //使用缩写,不易理解
    String n;
      
    float h;
      
    float a;
      
    int m;
}

Example 3: You can not use only keywords, but can include keywords
What are keywords
such as int, short, class java these words have been given a special meaning, these are the keywords. It can not be directly used as a variable name
, such as class
int. 5 = class;
class keywords
but may comprise a digital
, such as

int class5 = 5;
public class HelloWorld {
  
    public static void main(String[] args) {
  
        int class5 = 5;
         
    }
}

Example 4: Keyword list 1

Keyword list 1
Example 5: Keyword list 2

Keyword list 2
Example 6: Chinese also can be used to name variables
Chinese also can be used to name variables
, but in practical work. . . Do not do it. . .

public class 苍老师 {
    public void 开展教学工作(){
        System.out.println("雅蠛蝶");
    }
     
    public static void main(String[] args) {
        苍老师 苍 = new 苍老师();.开展教学工作();
    }
}
Published 32 original articles · won praise 182 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44092440/article/details/102964174