I want to declare all of them null
. Am I doing someting wrong or is this the right method?
String a = null, b = null, c = null, d = null;
(Is there any more compact syntax for doing this?)
You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.
Declaring and Assigning Variablesint a, b, c; You can also assign multiple variables to one value: a = b = c = 5; This code will set c to 5 and then set b to the value of c and finally a to the value of b .
You can declare several variables in one declaration statement, specifying the variable name for each one, and following each array name with parentheses. Multiple variables are separated by commas.
In Java, multiple variables can be initialized in the initialization block of for loop regardless of whether you use it in the loop or not.
Yep. That's the way to do it.
You could also do
String a, b, c, d;
a = b = c = d = null;
The line below however won't compile:
String a = b = c = d = null; // illegal
(Note that if these are member variables, they will be initialized to null
automatically.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With