1. In Java, each variable must have a type, in the variable declaration, the type must precede the variable name. Examples are as follows:
Double salary; int vacationdays; Long earthpopulation; Boolean done;
Each declaration must end with a semicolon, and the declaration is a complete statement, and each complete statement in Java must end with a semicolon.
2. Variable naming rules
Variable names must start with a letter, consist of letters and numbers, and letters in Java refer to Unicode characters.
Reserved words cannot be used.
Case sensitive.
Specification
You can declare multiple variables in a row, but not recommended, and not readable.
Do not name two different variables in case of differences
Note: You can check with the Isjavaidentifierstart and Isjavaidentifierpart method in the Character class (Java.lang.Character).
3.4.1 Variable Initialization
Declaring a variable as soon as possible using an assignment statement for display initialization, not using uninitialized variables, the following statements compile errors in Java
int// error--variable not initialized
Declarations and initializations can be written on a single line or multiple lines.
The statement should be written as far as possible in the first place.
Java does not distinguish between declarations and definitions, unlike C + + +.
Declaring a variable in Java does not actually allocate storage space to the variable, which is equivalent to a pointer declaration in C, just registering a
int A in Java, which is equivalent to int * A in C;
3.4.2 constant (constant)
In Java, you use final to specify that a variable is a constant, not a const in C, but a const is still a reserved word for Java.
Public class constants{ publicstaticvoid main (string[] args) { Finaldouble cm_per_inch = 2.54; double paperwidth = 8.5; double paperheight = one; System.out.println ("Paper size in centimeters:" + paperwidth * cm_per_inch + "by" + paperheight * cm_per_inch ); }}
The final keyword specifies that the variable can only be assigned once, once it is assigned, cannot be changed, and the habit is that the constant name is all uppercase.
corejavae10v1p3.4 3rd Java BASIC programming Structure-3.4 variables