In Java programming, every declared variable must be assigned a type. When declaring a variable, you should first declare the variable type, and then declare the variable name. The following demonstrates how variables are declared.
double salary; int age; Boolean op;
The first item is called the variable type, and the second item is called the variable name. A semicolon is required as it is the end of a Java statement.
Different variables of the same type can be declared on one line or on different lines. If they are to be declared on the same line, separate the different variables with commas,
For example:
int studentNumber,people;
You can assign a value to the variable while declaring it, or you can assign a value after declaring it.
For example:
int a=1; //声明时赋值 int a; a=1; //声明后赋值
Note: During the running of the program, the value in the space changes, and this memory space is called a variable. For the convenience of operation, this space is given a name, called a variable name, and the value in the memory space is the variable value. Therefore, after applying for memory space, the variable may not have a value. If you want the variable to have a value, you must put a value in it.
For example:
"int x;" A variable is defined but no value is assigned, that is, memory space is applied for but no value is placed; "int x=5; " Not only did it apply for memory space, but it also put a value, which is 5.
Note: For variables without assignment, the system will initialize them according to the following default values.
Declaring a variable is just like defining a variable, but the name is different. It has the same meaning as calling method C in Java and calling it function.
The above is the detailed content of How to declare variables in java. For more information, please follow other related articles on the PHP Chinese website!