variable
A variable is the basic storage unit in a Java program, and its definition mainly includes three parts: variable name, variable type, and variable scope.
variable name:
- is a valid identifier.
- Java is case-sensitive for variable names, variable names cannot start with numbers, and cannot be reserved words.
- Variable names should have a certain meaning to increase the readability of the program.
Variable type: It can be any data type.
Variable scope:
- The scope in which the variable can be accessed.
- Variables can only be used within scope.
- According to scope, variables can be divided into: local variables, global variables, class variables and method parameter variables.
- Global variables can be accessed throughout the entire file or even the entire program.
- A local variable is a variable declared in a method or method code, and its scope is the code block where it is located (the entire method or a certain block of code in the method).
- Class variables are declared in the class, not in a method of the class, and its scope is the entire class.
- The scope of method parameters is the current method, similar to local variables.
The variable declaration format is: Example
int a, b, c;
double d1, d2=0. 0;
float e = 2.718281828f;
Separate multiple variables with commas
constant
A quantity whose value does not change is called a constant. It is distinguished into different types.
- String constants: parts enclosed in double quotes. For example "123", "abc"
- Integer constant: write the number directly (without decimal point). eg 123, -123, 0
- Floating point constants: have decimals. eg 2.5, -3.1, 0.0
- Character constants: A single character enclosed in single quotes. eg 'A', '1', 'you', 'a'
- Boolean constants: only true, false
- Empty constant: null (no data)