Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String array: 'The value changed at 'i++' is never used'

Tags:

java

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]?

like image 767
Nithin Avatar asked Nov 29 '22 13:11

Nithin


2 Answers

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.

like image 147
Marcin Orlowski Avatar answered Dec 19 '22 22:12

Marcin Orlowski


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);
like image 41
AskNilesh Avatar answered Dec 19 '22 22:12

AskNilesh