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
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › lang › Integer.html
Integer (Java Platform SE 8 )
April 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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › integer-intvalue-method-in-java
Integer intValue() Method in Java - GeeksforGeeks
July 11, 2025 - --> java.lang Package --> Integer Class --> intValue() Method · Syntax: public int intValue() Return Type: A numeric value that is represented by the object after conversion to the integer type.
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › int-vs-Integer-java-difference-comparison-primitive-object-types
Integer vs. int: What's the difference?
The Integer class allows conversion to float, double, long and short, while the int doesn’t. The Integer consumes slightly more memory than the 32-bit Java int.
🌐
Reddit
reddit.com › r/learnjava › when to use int vs integer ?
r/learnjava on Reddit: When to use int vs Integer ?
August 8, 2021 -
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.

Find elsewhere
🌐
LabEx
labex.io › tutorials › java-how-to-convert-integer-to-int-in-java-420415
How to convert Integer to int in Java | LabEx
Learn efficient techniques to convert Integer wrapper class to primitive int in Java, covering key conversion methods, best practices, and practical usage scenarios for developers.
🌐
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.
🌐
Blogger
javahungry.blogspot.com › 2021 › 11 › int-to-integer.html
int to Integer Java | Java Hungry
public class IntToInteger2 { public static void main(String args[]) { // Initializing int variable int i = 200; // Convert int to Integer using new keyword Integer intObj = new Integer(i); // Displaying Integer System.out.println("Integer is: " + intObj); } } Output: Integer is: 200 That's ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › difference-between-an-integer-and-int-in-java
Difference between an Integer and int in Java with Examples - GeeksforGeeks
July 11, 2025 - In Java, int is a primitive data type while Integer is a Wrapper class. int, being a primitive data type has got less flexibility. We can only store the binary value of an integer in it. Since Integer is a wrapper class for int data type, it gives us more flexibility in storing, converting and manipulating an int data. Integer is a class and thus it can call various in-built methods defined in the class. Variables of type Integer store references to Integer objects, just as with any other reference (object) type.
🌐
Tutorialspoint
tutorialspoint.com › java › lang › integer_intvalue.htm
Java - Integer intValue() method
Python TechnologiesDatabasesComputer ... QualityManagement Tutorials View All Categories ... The Java Integer intValue() method returns the value of this Integer as a int....
🌐
Coderanch
coderanch.com › t › 378445 › java › Convert-Integer-int
Convert Integer[] to int[] (Java in General forum at Coderanch)
The existance of primitive types is a language defect, which is why the require 'workaround' is counter-intuitive. ... Hello Tony, thank you very much for your reply. I've feared that there would be no "compact" solution. Regards, Hans ... I agree, there is no "compact" solution in the API, AFAIK. I was playing around with some java.util stuff and I wrote the following code.
🌐
Processing Forum
forum.processing.org › topic › how-do-i-convert-an-int-array-to-an-integer
How do i convert an int array to an integer? - Processing Forum
for( int temp=0 · ; temp < theDigits · .length; temp++){ result*=10; result+=theDigits[temp]; } tfguy44 · 1 year ago · This assumes, of course, that the numbers you have in the array are digits! Leave a comment on tfguy44's reply · PhiLho · 1 year ago · There can be several strategies for converting an array of integers to a single integer: just pick one, sum them up as tfguy44 shown, but you can also compute the average, the median value, and so on...
🌐
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
🌐
InterviewBit
interviewbit.com › java-interview-questions
90+ Core Java Interview Questions and Answers | Freshers & experienced
But we can even say that Java is ... like int, float, char, boolean, double, etc. Now for the question: Is Java a completely object-oriented programming language? We can say that - Java is not a pure object-oriented programming language, because it has direct access to primitive data types. And these primitive data types don't directly belong to the Integer ...
Published   May 4, 2026
🌐
Coderanch
coderanch.com › t › 395749 › java › int-Integer-object-conversions
int to Integer object conversions (Beginning Java forum at Coderanch)
February 29, 2004 - You cannot assign an int to an Integer object. You can construct an Integer object using an int value by using the constructor: so I can do this: So what you will want to do is iterate through your array, and for each element in the array, create an Integer object and add that object to your ...
🌐
Kotlin
kotlinlang.org › docs › basic-syntax.html
Basic syntax overview | Kotlin Documentation
// Prints a message to request input println("Enter any word: ") // Reads and stores the user input. For example: Happiness val yourWord = readln() // Prints a message with the input print("You entered the word: ") print(yourWord) // You entered the word: Happiness · For more information, see Read standard input. A function with two Int parameters and Int return type:
🌐
Android Developers
developer.android.com › api reference › integer
Integer | API reference | Android Developers
Skip to main content · English · Deutsch · Español – América Latina · Français · Indonesia · Polski · Português – Brasil · Tiếng Việt · 中文 – 简体