As already written elsewhere:
- For Java 1.5 and later you don’t need to do (almost) anything, it’s done by the compiler.
- For Java 1.4 and before, use
Integer.intValue()
to convert from Integer to int.
BUT as you wrote, an Integer
can be null, so it’s wise to check that before trying to convert to int
(or risk getting a NullPointerException
).
pstmt.setInt(1, (tempID != null ? tempID : 0)); // Java 1.5 or later
or
pstmt.setInt(1, (tempID != null ? tempID.intValue() : 0)); // any version, no autoboxing
* using a default of zero, could also do nothing, show a warning or …
I mostly prefer not using autoboxing (second sample line) so it’s clear what I want to do.