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).
android - How to convert int to Integer - Stack Overflow
Casting from int to Integer.
How to convert Integer[] to int[] array in Java? - Stack Overflow
Type Converting from Scala to Java
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?
You can use Stream APIs of Java 8
int[] intArray = Arrays.stream(array).mapToInt(Integer::intValue).toArray();
If you can consider using Apache commons ArrayUtils then there is a simple toPrimitive API:
public static double[] toPrimitive(Double[] array, double valueForNull)Converts an array of object Doubles to primitives handling null.
This method returns null for a null input array.