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.
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.
Note that comma-separated initialization only works for variables of the same declared type. If you need to declare variables of different data types, you must use separate statements for each:
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.
You can also initialize variables of the same type to different values in a single statement:
int a = 1, b = 2, c = 3;