How to assign a value to a variable in Java?
In Java, you can use the equals sign (=) to assign values to variables. The syntax for assignment is: variable name = value. For example:
int num = 10; // 给整型变量num赋值为10
String name = "John"; // 给字符串变量name赋值为"John"
boolean flag = true; // 给布尔型变量flag赋值为true
Additionally, it is possible to assign the values of other variables to a new variable. For example:
int a = 5;
int b = a; // 将变量a的值赋值给变量b
Variables can also be assigned values using expressions. For example:
int sum = 2 + 3; // 将表达式2 + 3的结果赋值给变量sum