How to define a variable in Java?

In Java, a variable can be defined using the following syntax:

// 定义一个整型变量
int number = 10;

// 定义一个字符串变量
String name = "John";

// 定义一个布尔变量
boolean isTrue = true;

// 定义一个双精度浮点数变量
double price = 19.99;

When defining a variable, it is necessary to specify the data type of the variable and then assign it an initial value. The variable name can be any valid identifier, but it must adhere to naming conventions.

Leave a Reply 0

Your email address will not be published. Required fields are marked *