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.

Answer from user85421 on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-intvalue-method-in-java
Integer intValue() Method in Java - GeeksforGeeks
July 11, 2025 - Case 1: For a positive integer · Example: Java · // Java Program to Illustrate // the Usage of intValue() method // of Integer class // Importing required class/es import java.lang.*; // Main class public class GFG { // Main driver method ...
Discussions

android - How to convert int to Integer - Stack Overflow
I just wanted to share stackoverflow.com/q/3131136/384674 for "You should not call constructor..." 2018-12-15T20:14:14.3Z+00:00 ... Simon Z. Over a year ago · Just for curiosity, is Integer value = (Integer) intValue; a bad practise? Or, it's not recommended for int to Integer conversion? More on stackoverflow.com
🌐 stackoverflow.com
Casting from int to Integer.
A line like this Integer bigA = a; is converted to something like Integer bigA = Integer.valueOf(a); You can read the docs of the valueOf method here . It says that instances with values from -128 to 127 will always be cached. Meaning if you call valueOf with a value in that range you will always get the same Integer instance for one int value. The == operator for objects (like Integers) will compare instances, not logical values of these instances. So it will return true only if its the same instance of the Integer class. More on reddit.com
🌐 r/javahelp
10
17
June 12, 2022
How to convert Integer[] to int[] array in Java? - Stack Overflow
Is there a fancy way to cast an Integer array to an int array? (I don't want to iterate over each element; I'm looking for an elegant and quick way to write it) The other way around I'm using More on stackoverflow.com
🌐 stackoverflow.com
Type Converting from Scala to Java
See if this helps https://docs.scala-lang.org/overviews/collections-2.13/conversions-between-java-and-scala-collections.html Iirc it just allocates one object to be a wrapper around the original. More on reddit.com
🌐 r/scala
5
3
January 20, 2020
🌐
Blogger
javahungry.blogspot.com › 2022 › 02 › integer-to-int.html
Integer to int in Java | Java Hungry
According to Oracle docs, we can convert Integer object to primitive data type int using Integer's class intValue() method.
🌐
LabEx
labex.io › tutorials › java-how-to-convert-integer-to-int-in-java-420415
How to convert Integer to int in Java | LabEx
This tutorial explores the essential techniques and methods for seamlessly transforming Integer objects to primitive int values, providing clear insights into type conversion strategies in Java. In Java, int and Integer are two distinct types with fundamental differences.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "java howtos" › "how to convert integer to int in java"
How to Convert Integer to Int in Java | Delft Stack
February 12, 2024 - Before diving into the conversion methods, it’s essential to understand the distinction between Integer and int in Java. Integer is a wrapper class that encapsulates an int value, providing additional methods and functionality. On the other hand, int is a primitive data type representing a 32-bit signed integer. In Java, we can convert an Integer object to a primitive int type using auto-unboxing.
🌐
Study.com
study.com › business courses › business 104: information systems and computer applications
Converting Integer to Int in Java - Lesson | Study.com
March 8, 2018 - The intValue( ) function can be used to convert classes to primitives. Java will perform unboxing automatically. If you pass an Integer object to a function expecting an int parameter, it will convert it down to int.
🌐
Blogger
javahungry.blogspot.com › 2021 › 11 › int-to-integer.html
int to Integer Java | Java Hungry
Using new keyword Read Also: How to convert char to Character in Java · You can use Integer's class valueOf() method to convert int to Integer as shown below:
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
July 21, 2026 - Java™ Platform Standard Ed. 8 ... The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int. In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › number_intvalue.htm
Java Number intValue() Method
The Java Number intValue() method converts the value of the specified number into an int value. Here, we say a 'number' as it can be in any form − short, long, float, double. An int value is a primitive data type that only stores integer values.
🌐
Delft Stack
delftstack.com › "delft stack" › "howto" › "java howtos" › "how to convert int to integer in java"
How to Convert Int to Integer in Java | Delft Stack
February 12, 2024 - This variable represents the primitive integer that we aim to convert to its wrapper class. Moving on to the second step, we make use of auto-boxing. By directly assigning the int variable primitiveInt to an Integer variable named wrapperInteger, Java automatically wraps the primitive int value in an Integer object, simplifying the conversion process.
🌐
Mkyong
mkyong.com › home › java › java – convert int[] to integer[] example
Java – Convert int[] to Integer[] example | mkyong.com
February 22, 2014 - To save you time, uses the ArrayUtils class from the Apache common library, it does the same thing. ... package com.mkyong.test; import org.apache.commons.lang3.ArrayUtils; public class ArrayConvertExample { public static void main(String[] args) { int[] obj = new int[] { 1, 2, 3 }; Integer[] newObj = ArrayUtils.toObject(obj); System.out.println("Test toObject() - int -> Integer"); for (Integer temp : newObj) { System.out.println(temp); } Integer[] obj2 = new Integer[] { 4, 5, 6 }; int[] newObj2 = ArrayUtils.toPrimitive(obj2); System.out.println("Test toPrimitive() - Integer -> int"); for (int temp : newObj2) { System.out.println(temp); } }
🌐
YouTube
youtube.com › jakubication
How To Convert Int To Integer In Java - YouTube
How To Convert Int To Integer In Java? This tutorial answers how to convert an int to an integer in Java. Using the Integer constructor to convert an int to ...
Published: March 25, 2024
🌐
W3Schools
w3schools.com › java › java_type_casting.asp
Java Type Casting
Java Examples Java Videos Java Compiler Java Exercises Java Quiz Java Code Challenges Java Practice Problems Java Server Java Syllabus Java Study Plan Java Interview Q&A ... Type casting means converting one data type into another. For example, turning an int into a double. ... Widening Casting (automatic) - converting a smaller type to a larger type size byte -> short -> char -> int -> long -> float -> double
🌐
YouTube
youtube.com › watch
How to convert between an int and an Integer object in Java - YouTube
In this video we will learn how to convert between an int and an Integer object in Java.
Published: December 29, 2019
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › lang › Integer.html
Integer (Java SE 17 & JDK 17)
April 21, 2026 - The Integer class wraps a value of the primitive type int in an object. An object of type Integer contains a single field whose type is int. In addition, this class provides several methods for converting an int to a String and a String to an int, as well as other constants and methods useful when dealing with an int.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › int-vs-Integer-java-difference-comparison-primitive-object-types
Java Integer vs. int: Types, Performance, and Usage
Some of the helpful methods of the Java Integer class include: MIN_VALUE and MAX_VALUE to help determine the range. ... Since the int data type simply points at 32 bits of memory, with none of the overhead of object allocation that the Integer class requires, applications that use int and not Integer types run faster. However, there are times when the JVM converts int data types into Integer instances to support collection classes such as ArrayLists or HashMaps.
Published: March 26, 2026
🌐
Coderanch
coderanch.com › t › 402111 › java › casting-int-Integer
casting int to Integer (Beginning Java forum at Coderanch)
January 23, 2006 - Originally posted by Ernest Friedman-Hill: In Java 5, you can do that assignment, actually, via "autoboxing".: The compiler will automatically change it into something like this: a[i] = new Integer(num); Of course, in earlier Java versions, you can do this manually.