How are Java variables declared?
In Java, you can use keywords to declare variables. Declarations typically include the type and name of the variable.
Syntax format:
数据类型 变量名;
In this case, the data type can be either a primitive data type provided by Java, such as int, double, boolean, etc., or a custom class type.
For example, we can define an integer variable.
int num;
Declare a string variable:
String name;
You can also assign an initial value to a variable when defining it.
int age = 18;
String message = "Hello World!";
When defining a variable, you can also use the keyword “final” to modify the variable, indicating that the value of the variable cannot be changed once it is assigned, making it a constant.
final double PI = 3.14;