1. Variables
A variable is a pronoun that refers to the data in memory.
A variable is a quantity that can be changed----> refers to different data.
2. Use of variables
Variables must be declared before use:
Syntax: data type variable name;
int A;
You can declare multiple variables of the same type in a single statement
such as: int a,b,c;
int a=1,b,c=2;
3. Naming of variables
Provisions:
- Can contain only letters, numbers, _,$, and cannot start with a number
- Strictly case-sensitive
- Cannot use keywords (words that have been used in Java)
Suggestions:
- Chinese named variables are not recommended
- Better "See the name"
- Hump Naming method: First letter lowercase, the remaining letters capitalized.
4. Initialization of variables
Java syntax rules: variables must be initialized before they are used
Initialize two ways:
- Declaring simultaneous initialization
int a=5;
- First declare after initialization
int b;b=8;
5. Use of variables
Must match the data type:
such as: int a=2.5;//error, type mismatch
6. Scope of variables
- From the beginning of the variable declaration to the nearest curly brace that contains it.
- Scope overlap, variables cannot have duplicate names.
7. The difference between a member variable and a local variable
Local variables:
- Defined in the method.
- Without a default value, you must set the default value yourself.
- When the method is called, it exists in the stack, and is purged from the stack at the end of the method call.
Member variables:
- Defined in the class, outside of the method.
- There is a default initial value that can be initialized without explicit initialization.
- After all classes are instantiated, they exist in the stack, the objects are recycled, and the member variables are invalidated.
Variables in the Java language