Initializing multiple variables to the same value in Java
In Java, you can initialize multiple variables to the same value in a single statement by separating the variables with a comma. Here is an example of how you can do this:
int x = 10, y = 10, z = 10;
This code initializes three int
variables, x
, y
, and z
, to the value 10.
You can also use this syntax to initialize variables of different data types to the same value. For example:
int x = 10;
double y = 10.0;
long z = 10L;
This code initializes an int
variable, x
, to the value 10, a double
variable, y
, to the value 10.0, and a long
variable, z
, to the value 10L.
Note that you can only use this syntax to initialize variables to the same value. If you want to initialize variables to different values, you will need to use separate statements for each variable.