Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set multiple int's equal to the same number ? Java

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.

like image 286
user2893243 Avatar asked Oct 18 '13 05:10

user2893243


People also ask

How do you assign multiple variables to the same value in Java?

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 .

Can we declare multiple variables in a single line in Java?

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.

How do you declare two integers in Java?

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).


2 Answers

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;
like image 114
Aditya Vikas Devarapalli Avatar answered Oct 06 '22 00:10

Aditya Vikas Devarapalli


Initialize all the values, then set the values.

int index, r, g, b;
index = r = g = b = 0;
like image 31
hexacyanide Avatar answered Oct 05 '22 23:10

hexacyanide