Java Variable Naming Rules Guide
The rules for naming variables in Java are as follows:
- The variable name must begin with a letter, underscore, or dollar sign $.
- Other parts of the variable name can consist of letters, numbers, underscores, or the dollar sign $.
- The variable name cannot be a Java keyword or reserved word.
- Variable names are case-sensitive, for example, “num” and “Num” are considered different variable names.
- Variable names should be descriptive and clearly express the purpose of the variable.
- The variable name should not exceed 255 characters.
- To improve the readability of the code, it is recommended to use camelCase naming convention for variables, where the first letter is lowercase and subsequent words have their first letter capitalized, for example: myVariable.
- It is recommended to use all capital letters and underscores to separate words for constants, such as: MAX_VALUE.
Here are some examples of valid variable names:
age as integer;
firstName as string;
averageScore as double;
MAX_VALUE as constant integer;
isReady as boolean;