Identifiers and naming rules based on java language

Identifier

  • The character sequence used in Java to name various variables , methods and classes is called identifier
  • Tips : wherever you can name it, it is called an identifier
  • Define legal identifier rules :
  • It is composed of 26 English letters in upper and lower case, 0-9, _ or $ can not start with a number.
  • Keywords and reserved words cannot be used, but keywords and reserved words can be included.
  • Java is strictly case-sensitive, and the length is unlimited.
  • Identifier cannot contain spaces

The following is the code description: (Help everyone understand the meaning of each sentence)

class IdentifierTest{
    
    
     public static void main(String[] args){
    
    
     system.out.println("这是一个测试类名")
}
}
class Hello1_${
    
    
}
/*这是以字母开头的类名,符合命名规范*/
class 1Hello{
    
    
}
/*这里以数字开头明显是错误的,不符合命名规范*/

Insert picture description here

class static{
    
    
}
/*这里使用了java关键字开头明显是错误的,不符合命名规范*/
class static1{
    
    
}
/*假如这里使用了static1可不可以,当然是可以了,因为它不是关键字了*/

Insert picture description here
Can it be written as Static? think for a while! Of course it is possible, because the keywords are all lowercase. The java language is strictly case sensitive.

class Static{
    
    
}

Why can't the identifier contain spaces?
Look at the following code:

class abcdefg{
    
    
}
class abc defg{
    
    
}

The first one is obviously correct, and the second one adds a space, so whether the class name is called abcdefg or abc or defg. Get it here. You can try to compile it.

Finally, if you do not follow the above rules, the compilation will not pass, and the bytecode file will not be generated, let alone run.

The naming conventions in Java (if you don’t comply with the following conventions, the compilation can be passed; but it is recommended that you strictly abide by it, because the code is not for yourself to see, it is more conducive to others to understand and distinguish, and it is very beautiful)

  • Package name : All letters are lowercase when multiple words are formed: xxxyyyzzz

  • Class name , interface name : when multiple words are formed, the first letter of all words is capitalized: XxxYyyZzz

  • Variable name , method name : when multiple words are formed, the first letter of the first word is lowercase, and the second word starts with uppercase of each word: xxxYyyZzz

class IdentifierTest{
    
    
public static void main(String[] args){
    
    
int myNumber = 1001;
System.out.println(myNumber);
}
}

operation result:
Insert picture description here

  • Constant name : All letters are capitalized. When there are multiple words, each word is connected with an underscore: XXX_YYY_ZZZ

Note 1: When naming, in order to improve readability, it should be as meaningful as possible, "see the name to know the meaning".

class User{
    
    
}

Note 2: Java uses the unicode character set, so the identifier can also be declared in Chinese characters, but it is not recommended.

class IdentifierTest{
    
    
public static void main(String[] args){
    
    
int myNumber = 1002;
System.out.println(myNumber);
int 号码 = 1003;
System.out.println(号码);
}
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_45552871/article/details/114225273