1

Is this the proper syntax in order to set the parameters of a previously generated preparedstatement? Which occurs first, the addition of 1 to the variable i, or the usage of the variable in setting the parameter?

int i=1;
for (TagInfo tag : scannedTags){
    //Pull the tag from the dbo.Tags table that matches the SiteID and TagOffSet values of the current tag in the alarm_tags list 
    //Set parameters for the prepared statement
    dbStmtTag.setInt(i++, tag.getSiteID());
    dbStmtTag.setInt(i++, tag.getTagOffset());
}

If the order of operations is that the value is incremented first, I would assume I can just add 1 AFTER setting the parameter. I'm merely asking for the sake of brevity in my code.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
TheFunk
  • 981
  • 11
  • 39
  • Why don't you just try it and prove it to yourself? It would have taken about the same amount of time as asking, and you would probably remember the result better because "learning by doing" works better than "learning by hearing". – Gord Thompson Jun 07 '16 at 14:48
  • I'd have to write an entire test application that talks to a test database, which I would need to create. I should probably do that soon enough anyway, but i digress, I was just hoping for a quick answer on this one. – TheFunk Jun 07 '16 at 15:09
  • 2
    That’s the fundamental difference between `++i` and `i++`, the former evaluates to the new value, the latter to the old value. – Holger Jun 07 '16 at 15:25
  • 1
    http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html#h101 – Holger Jun 07 '16 at 17:08
  • OOOH!!! I like that! I did not know this! I just never used ++i ever, because it looked funny to me. – TheFunk Jun 07 '16 at 17:37

1 Answers1

2

[To test the behaviour of i++ for myself] I'd have to write an entire test application that talks to a test database, which I would need to create.

Nonsense. All you would need to do is ...

public static void main(String[] args) {
    try {
        int i = 1;
        System.out.printf("i is %d%n", i);
        System.out.printf("i++ returned %d%n", i++);
        System.out.printf("i is now %d%n", i);
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

... which produces ...

i is 1
i++ returned 1
i is now 2
Gord Thompson
  • 116,920
  • 32
  • 215
  • 418
  • 1
    Trying something does not prove that this is a guaranteed behavior. It only proves that it *may* happen. – Holger Jun 07 '16 at 17:06
  • 1
    @Holger - It proves that it *will* happen, at least under the conditions depicted by the test code. To claim otherwise is to say that "we can never be sure of what any of our code will *actually do*". – Gord Thompson Jun 07 '16 at 17:21
  • 2
    There is something called [Java Language Specification](https://docs.oracle.com/javase/specs/jls/se8/html/index.html) which is authoritative regarding what you can rely on and what is just coincidence (or even a bug). The fact that this specific operator does the intended thing, does not make “*just try it*” a valid way of verifying assumptions about Java language constructs. – Holger Jun 07 '16 at 17:28
  • I appreciate the responses fellas. The code is now looking extra nice! Now I just need to figure out how much faster multiple ORs would be as opposed to a batch of updates. We'll see! – TheFunk Jun 07 '16 at 17:39