Home >Java >javaTutorial >What are the naming rules for java variables?
Java variable naming rules are: 1. The variable name must start with a letter, underscore, or dollar sign; 2. The variable name can only contain letters, numbers, underscores, and dollar signs; 3. The variable name cannot be a Java key word; 4. Variable names should be descriptive; 5. Camel case naming; 6. Constant naming rules; 7. Local variable naming rules; 8. Class and interface naming rules; 9. Package naming rules; 10. Special type variables name. Detailed introduction: 1. The variable name must start with a letter, underscore or dollar sign. This is the Java variable naming rule and so on.
The operating system for this tutorial: Windows 10 system, DELL G3 computer.
Java variable naming rules are a rule agreed by the Java programming language for naming variables, methods, classes, etc. The following are some key points of Java variable naming rules:
1. The variable name must start with a letter, underscore, or dollar sign: This is the basic requirement of Java variable naming rules. The first character of a variable name cannot be a number, but can only be a letter, underscore, or dollar sign.
2. Variable names can only contain letters, numbers, underscores and dollar signs: Java variable names can contain letters (both uppercase and lowercase), numbers, underscores and dollar signs, but Cannot start with a number.
3. The variable name cannot be a Java keyword: The variable name cannot be the same as the Java language keyword, such as int, float, for, while, etc.
4. Variable names should be descriptive: Good variable names should be able to clearly express the purpose and meaning of the variable, so that other developers can better understand the code when reading it. meaning.
5. Camel case naming method: For variable names composed of multiple words, it is recommended to use camel case naming method. There are two types of camel case nomenclature: lower camel case nomenclature (lowerCamelCase) and large camel case nomenclature (UpperCamelCase). Little camel case means that the first letter of the word starts with a lowercase letter, and the first letter of the following words is capitalized; big camel case means that the first letter of each word uses an uppercase letter.
6. Constant naming rules: Constants refer to variables that cannot be changed during program running. Constants are usually named using all uppercase letters, with words separated by underscores. For example: MAX_VALUE.
7. Local variable naming rules: Local variables are variables defined in a method, and their scope is limited to the method in which they are defined. The naming of local variables can be arbitrary, but it is best to clearly express the purpose of the variable.
8. Class and interface naming rules: The naming of classes and interfaces should be able to clearly express their meaning, and the names should use camel case nomenclature. If the class name consists of multiple words, the first letter of each word should be capitalized.
9. Package naming rules: Package is a mechanism used to organize and manage Java classes. Package names should be in lowercase letters, with dots separating words, and should clearly express the function or purpose of the package.
10. Naming of special types of variables: For some special types of variables, such as enumeration types, generic types, etc., they should be named according to the specific situation. In general, you should try to express the purpose and meaning of variables as clearly as possible.
In short, Java variable naming rules are to make the code more readable, maintainable and extensible. Following these rules can improve the readability and maintainability of your code, making it easier for other developers to understand and use the code. At the same time, some potential errors and problems can also be avoided. Therefore, when writing Java code, you should follow these rules and try to keep the code consistent and standardized.
The above is the detailed content of What are the naming rules for java variables?. For more information, please follow other related articles on the PHP Chinese website!