1.Java variables
1.1 Concepts of variables
- A storage area in memory;
- The region has its own name (variable name) and type (data type);
- The data of the region can be changed continuously within the same type;
1.1.1 Why you need to define variables
- Used for constant storage of the same type of constant and can be reused;
1.1.2 Defining the format of a variable
- Variable name of data type = initialization value;
1.1.3 Variable Use range
- The scope of the variable (valid between a pair of {}, "nearest principle");
1.2 Use of variables
1) Disclaimer
int A; Declares an integer variable named A;
int b,c,d; Represents a declaration of three integer variables, named b,c,d;
2) naming
- Can only contain numbers, captions, _ (underscore), and $ (dollar sign), and cannot start with a number;
- Strictly case-sensitive
- Cannot use keywords
- Can be named in Chinese, but not recommended
- Naming rules: suggested "Hump naming", "See the name", such as defining a variable to indicate student score: Studentscore
3) Initialization
Simultaneous initialization of declarations:
Declare and Initialize first:
int a;a = 250;
4) Use
- The operation of a variable is to manipulate the number it has stored.
int a = 5; Declare the variable A and assign a value of 5;int B = a+10; Declare variable B, take out the value of a of 5, plus 10, and assign to BSystem.out.println (b); The value of output B is 15a = A+b; Remove the value of a of 5, then remove the value of B 15, and then the two number and re-assigned to ASYSTEM.OUT.PRINTLN (a); The value of output A is 20
- The operation of the variable must match the data type
int a = 3.14;//error: Type mismatch, the type Mismatch:cannot convert from double to int
- Variables must be declared and initialized before they are used
System.out.println (m); Compile error, m not declared int m; System.out.println (m); Compilation error, m not initialized
Java variables and basic data types