4

Here's my code:

String[] queries = new String[2];
int i = 0;
Boolean result;
queries[i++] = "<query 1>";
queries[i++] = "<query 2>"; //Warning shown here
result = dbOpenHelper.ExecuteMyTransaction(queries);

The second i++ gets highlighted and the warning 'The value changed at 'i++' is never used' is shown. This code was written by another person, and to my knowledge, here <query 1> and <query 2> get assigned to queries[1] and queries[2] respectively, but then it must show an error, as the array is of size 2. There's no error and this kind of confuses me on what's happening here. Can I safely remove the second assignment, or change the first one to queries[i]?

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
Nithin
  • 221
  • 3
  • 14
  • 2
    use this: queries[i] = ""; – Sarthak Mittal Aug 30 '17 at 10:21
  • 3
    *`` and `` get assigned to `queries[1]` and `queries[2]`* array indices starts in 0, the values will be assigned to `queries[0]` and `queries[1]`. See also [post increment and pre increment](https://stackoverflow.com/questions/2371118/how-do-the-post-increment-i-and-pre-increment-i-operators-work-in-java) – Guy Aug 30 '17 at 10:29

5 Answers5

8

The code is correct and you can safely ignore this warning or replace underlined i++ with just i.

This warning simply indicates that as there's no further use of i variable in that scope, so incrementing its value or not makes no effect and is simply pointless.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Does that mean i is incremented after the assignment is done? – Nithin Aug 30 '17 at 10:22
  • 3
    Yes. `i++` means post increment, which means first the `i` value is taken and used, then is incremented. If you want it incremented before use, then `++i` (pre increment) should be used instead. – Marcin Orlowski Aug 30 '17 at 10:22
6

i++ means post increment, which means first the i value is taken and used, then is incremented.

your code is correct you can ignore this warning and try below code to get out of this warning

String[] queries = new String[2];
int i = 0;
Boolean result;
queries[i++] = "<query 1>";
queries[i] = "<query 2>"; //change this here
result = dbOpenHelper.ExecuteMyTransaction(queries);
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
4

The second i++ could as well be i because i no longer is inspected. The post-increment i++ would return the current value of i and then increment i.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
4
  • but then it must show an error, as the array is of size 2.

If you try and access queries[2], you would certainly get to know What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? . Though which is not the case with your current code.

  • There's no error and this kind of confuses me on what's happening here.

The array bounds are not checked at the compile time but runtime as the size is allocated itself at runtime. More details on Why isn't the ArrayIndexOutOfBoundsException a compile time error?

  • Can I safely remove the second assignment, or change the first one to queries[i]?

You can remove the assignment or as mentioned by others use post increment operator only in the first assignment as

queries[i++] = "<query 1>"; // index accessed is 0 here
queries[i] = "<query 2>"; // i=1 here
  • The value changed at 'i++' is never used'

This is a compile time warning since the compiler expects you to perform an operation on i further in the same scope where you are manipulating its value.

Naman
  • 27,789
  • 26
  • 218
  • 353
1

++i increments i and then uses the variable. (Pre-increment)

i++ uses and then increments the variable.(Post-increment)

In your codes, It's post increment operation.

queries[i++] = "<query 1>"; // It's equivalent to queries[0] = "<query 1>";
queries[i++] = "<query 2>"; // It's equivalent to queries[1] = "<query 2>";

So your code should be working fine as expected. However, it's always good for 2nd query to be used as queries[i] = "<query 2>";

nagendra547
  • 5,672
  • 3
  • 29
  • 43
  • 1
    It doesn't fail to compile, but that doesn't change the fact that `i++` in `queries[i++] = ""` is pointless. – Tom Sep 05 '17 at 14:43
  • 1
    As mentioned by other answers, it will be good if he can change it to queries[i] = ""; But, as this code is legacy, so it's not doing any harm as such. But it will be always good to change to queries[i] = ""; – nagendra547 Sep 05 '17 at 14:47