English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I just want to know what the % sign in java means? Let's say I put in 456 % 100 = 56, from where do I get the 56? Thanks

2006-08-31 13:44:06 · 5 answers · asked by Anonymous in Computers & Internet Programming & Design

5 answers

That is a modulo operation.

When you divide an integer by another integer you will have a remainder.

456 = 100(4) + 56

2006-08-31 13:49:23 · answer #1 · answered by Anonymous · 0 0

% is modulus. It's the remainder (56) if you divide 456 by 100. This % operator is very useful for getting multiples from a set of data, or for when you want to grab every nth item in a loop. For example, if you wanted to alternately colour every other row in a tabular output through JSP, you could use % 2 to get every other item in the loop.

Here's an example snippet:

int rowNumber = 1;
while (results.next()) {
String rowColour = "white";
rowColour = (rowNumber % 2 == 0) ? (bgColour) : (altColour);
tableString += "";
for (int j = 1; j <= metaData.getColumnCount(); j++) {
tableString += "" + results.getString(j) + "";
}
tableString += "";
rowNumber++;
}

2006-08-31 20:59:36 · answer #2 · answered by MJQ 4 · 0 0

% is modulus operator. suppose u have 3%2 then when u divide 3 by 2 u get remainder 1. so 3 % 2 is 1

2006-09-01 06:19:47 · answer #3 · answered by Shamim P 1 · 0 0

it appears that it rouds 4.5 to 5 or >>> it seems to be saying chop off the hundreds digit that is 4. That leaves 56. I don't have my Java books here to check.

2006-08-31 20:52:21 · answer #4 · answered by ? 4 · 0 0

% is math modulo, remainder of integer division; it is the same symbol for modulo in many languages.

2006-08-31 21:02:28 · answer #5 · answered by Andy T 7 · 0 0

fedest.com, questions and answers