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.
PreparedStatement
The question is showing the use of PreparedStatement. in this case, its setObject() method can be used instead of setInt(). It will do the appropriate conversion and accept an Integer, even it being null.
Another alternative, the setNull() method, but that requires first testing the value for null and passing the column type as second argument.
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.
PreparedStatement
The question is showing the use of PreparedStatement. in this case, its setObject() method can be used instead of setInt(). It will do the appropriate conversion and accept an Integer, even it being null.
Another alternative, the setNull() method, but that requires first testing the value for null and passing the column type as second argument.
Since you say you're using Java 5, you can use setInt with an Integer due to autounboxing: pstmt.setInt(1, tempID) should work just fine. In earlier versions of Java, you would have had to call .intValue() yourself.
The opposite works as well... assigning an int to an Integer will automatically cause the int to be autoboxed using Integer.valueOf(int).
Videos
int iInt = 10;
Integer iInteger = Integer.valueOf(iInt);
P.S. Answer edited due to comments pointing out an issue with initial suggested solution.
As mentioned, one way is to use
int original = 32;
Integer converted = new Integer(original);
But you should not call the constructor for wrapper classes directly. It is a poor practice to do so. Instead, use the methods pre-defined especially for this purpose.
So, the new code would look like this (recommended):
mBitmapCache.put(Integer.valueOf(R.drawable.bg1), object);
So i've been fiddling around with java, and encountered a problem, that I don't quite understand.
int a = 10;Integer bigA = a;Integer bigB = a;System.out.println(bigA == bigB);
This piece of code returns true, for every int below and including 127. Above that it returns false. How so?
import java.util.*;
class Main {
public static void main(String args[]) {
Integer[] a = {1,2};
int[] b = {1,2};
System.out.println(Arrays.toString(a));
System.out.println(Arrays.toString(b));
}
}Hi all, trying to learn Java from python background and run into some trouble.
So I have read that Integer is the object class and int is the binary primitive, and that Java compiler is now able to convert between them when nesessary.
But when should I use the primitive vs the object ? Generally speaking, all I can think of right now is that we should always use the primitives unless we may need to cast to another type in the future so then use object ones.