Java identifiers, variables and constants

1. Java identifiers:


Java identifiers have the following naming rules:
       1. Identifiers are composed of letters, numbers, "_", "$", and the first letter cannot be a number;
       2. Java keywords cannot be used as identifiers;
       3. The identifier has no length limit;
       4. The identifier is case-sensitive.


Suggested naming rules for identifiers:
      Specifications (classes, interfaces, method names, and variable names are in camel case, package names are always lowercase, constants are always uppercase, and are separated by _ when composed of multiple words), easy to
      read (short), and
      clear in meaning (see


Keyword: Also known
as reserved words, it is an identifier with a specific meaning specified by the Java language.
It has been used by the Java language itself and cannot be used as variable names, method names, class names, and package names.


Second, Java data types:
Java has eight basic data types, namely: byte, short, int, long, double, float, boolean, char.
The data type determines the type of data stored in memory and the amount of memory required.


Note:
         Since different integer data types are allocated in different sizes in memory, we need to think carefully about which data type we use in development, which is related to the performance of the program.
For example, the age of a person will not exceed 200 years old, so it is more appropriate to choose short. The same should be true when choosing floating point.


Conversion between data types:
1. Java data type conversion (whether it is a basic data type or a reference type) is divided into:
        automatic type conversion and
        forced type conversion
      2. When assigning a data type representing a small range to a data type representing a large range, Java automatically uses the implicit type Complete data type conversion, ie:
      low ------------------------------------------------------ -------------------------------------------------- ------------------------------> high
      byte ------------> short/char - -------------> int ------------> long ------------> float --------- ---> double


3. When assigning a high-level variable to a low-level variable, explicit type conversion must be used. Display Conversion Format: (Type to convert) The value being converted.
For example: int x=(int)23.89;//The value of x is 23 long y=(long)34.98F; ;//The value of y is 34
4. Explicit type conversion (coercion) may result in loss of data precision or overflow.


Three, variables:
1. Variable overview: Java applies for data storage space by defining variables, and obtains or changes the stored value through the variable name.
2. Variable naming rules:
             variable names must follow the identifier naming rules;
             The first letter must be lowercase, and if it consists of multiple words, the first letter must be uppercase starting from the second word;
             variable names generally consist of nouns.
3. Variable definition: [access control character] [modifier] data type variable name [= initial value];


4. Constants
1. Constant overview: Constants are modified by the final keyword, and constants are variables with immutable values, that is, they cannot be changed Assignment (different from "value does not change"), constants must be initialized when they are declared
2. Constant definition:
[access control] [modifier] final data type constant name = initial value;
3. Example:
static final double PI = 3.1415926;


5. Difference between variable and constant light
1. Identifier naming difference:
        variable: under the premise of conforming to the identifier, the first letter is lowercase, and the first letter of each word after that is uppercase;
        constant: under the premise of conforming to the identifier , the left and right letters are capitalized, and multiple words are separated by underscores;
2. Whether the value can be changed again:
        variable: the value of the variable can be changed;
        constant: the value of the constant is not allowed to be changed again after initialization;
3. Whether to use the final keyword:
        Variables: must not have the final keyword;
        constants: must be modified with the final keyword;
4. Whether initialization is required:
        Variables: only local variables must be assigned a value (not initialization) before use;
        Constant: must be initialized at the time of declaration;



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325649641&siteId=291194637