One, constant
A Java constant is a value that cannot be changed once the value is assigned. Java constants are decorated with the keyword final. General constant names each word has uppercase letters, and multiple words are separated by underscores.
- Classification of constants
- Integer constants: All integer values
- Decimal constants: So the numeric value of the floating-point type
- Boolean constant: Only two, false and true
- Character constants: such as, ' 1 ', ' B ', ' C ' .....
- String constants: such as, "123", "Love", "ABDCD" ....
- NULL constant: Only one value
- Four representations of integers
- Binary: 0,1 composition, full two in one
- Octal: 0~7 composition, full eight in one, starting with 0
- Decimal: 0~9 composition, full 10 into a
- Hex: 0~9 and ABCdef, full 16 in one, start with 0x
Note: In Java, if you want a constant to be used in more than one method of a class, these constants are often referred to as class constants.
Second, the variable
In Java, there is a value that can be changed as the program runs, and the value is called a variable. In Java, each variable belongs to a type.
- Declaration of a variable
- The type to which the variable belongs is before the variable name: int A;
- The specification of a variable is a naming convention for identifiers
- Range of variables: between a pair of {}
- Declaration format: Data type variable name = initialization value;
- Initialization of variables
- After declaring a variable, you must initialize the display
- Using uninitialized variables, the program will error
Attention:
-
- In Java, you can place the declaration of a variable anywhere in the program.
- The declaration of a variable is as close as possible to where the variable was first used.
- When defining variables, consider the use period of the variables and release them as early as possible. For example, if a variable needs to be used only in a function, do not declare it as a class variable, etc.
Expand Knowledge Points:
- Explanation of the variable:
A variable is actually a stored area in memory, and the region has its own name and type, and the region's data can be constantly changed within the same type range.
Java is a strongly typed language with a strict type, with different bytes allocated for each type, so that one type of data cannot be used in other types. A type of memory can only be a type that uses or is smaller than the same type.
Variable name: a name defined to facilitate the use of this memory
Constants and variables for Java