I have the code int index, r, g, b;
and I want to set all of them equal to a certain number, I don't individually want to write index = 0; r = 0;
... and so on.
How do I do this in java?
index,r,g,b = 0;
doesn't work for me.
Declaring and Assigning Variables int 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 .
Like Java, we can declare and assign multiple variables in one line. In this tutorial, we'll addresses approaches to do that: Separating declarations by semicolons. Assigning two variables using a Pair object.
To declare (create) a variable, you will specify the type, leave at least one space, then the name for the variable and end the line with a semicolon ( ; ). Java uses the keyword int for integer, double for a floating point number (a double precision number), and boolean for a Boolean value (true or false).
use this line of code for initializing all the variables at once
r = g = b = index = 0;
or else initialize when you declare like:
int index=0, r=0, g=0, b=0;
Initialize all the values, then set the values.
int index, r, g, b;
index = r = g = b = 0;
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