HOWTO · Java
How to Convert Object to Int in Java
Learn four ways to get an int from an Object in Java: cast an Integer, use Number.intValue(), call Integer.intValue(), or parse integer text.
On this page
To get an int from an Object in Java, first identify the value’s runtime type. Cast and unbox it when it contains an Integer; use Number.intValue() for another numeric wrapper; or parse its text with Integer.parseInt(). These approaches are not interchangeable: an Object reference alone does not promise a numeric value.
Type conversion is one of the topic groups in the Java Tutorial, where the conversion guides are gathered in one place.
The examples below are unexecuted in the supplied evidence; their stated values follow from the shown Java conversions.
Convert Object to int in Java by Using Integer Wrapper Class
When the object contains an Integer, a cast followed by assignment to int both checks the wrapper type and unboxes its value. The method below returns the value 123 for the supplied object:
public class Main {
public static int objectToInt(Object obj) {
return (Integer) obj;
}
public static void main(String[] args) {
Object obj = Integer.valueOf(123);
int value = objectToInt(obj);
System.out.println("The converted int is " + value);
}
}
The cast requires the runtime value to be an Integer; another class causes ClassCastException. If obj is null, the cast yields null and unboxing it to int causes NullPointerException. Check the value or handle these cases if the input is not guaranteed.
Convert Object to int in Java by Using Number and intValue() Function
Use Number when the object can hold different numeric wrapper types, such as Integer, Long, or Double. The runtime value must still be a Number. intValue() returns an int; for values outside the int range or with a fractional part, conversion can lose information. In this example, the returned value is 123:
public class Main {
public static int objectToInt(Object obj) {
return ((Number) obj).intValue();
}
public static void main(String[] args) {
Object obj = Double.valueOf(123.9);
int value = objectToInt(obj);
System.out.println("The converted int is " + value);
}
}
This converts a numeric value; it does not parse a string such as "123". A non-Number object cannot be cast to Number.
Convert Object to int in Java by Using Integer and intValue() Function
This form is specifically for an object whose runtime value is Integer. It names the unboxing step explicitly with intValue(), though assigning the cast Integer directly to int usually does the same work automatically. The method returns 123 for this input:
public class Main {
public static int objectToInt(Object obj) {
return ((Integer) obj).intValue();
}
public static void main(String[] args) {
Object obj = Integer.valueOf(123);
int value = objectToInt(obj);
System.out.println("The converted int is " + value);
}
}
As with the direct cast, a different runtime type causes ClassCastException, and a null Integer causes NullPointerException when intValue() is called. Use the broader Number form when other numeric wrapper types are valid inputs.
Convert Object to int in Java by Using Integer.parseInt() Function
Parsing is for text that represents a decimal integer. Convert the object to text with toString(), then pass that string to Integer.parseInt(). For the string "123", parsing returns the int value 123:
public class Main {
public static int objectToInt(Object obj) {
return Integer.parseInt(obj.toString());
}
public static void main(String[] args) {
Object obj = "123";
int value = objectToInt(obj);
System.out.println("The converted int is " + value);
}
}
The text must be a valid decimal representation within the int range; otherwise Integer.parseInt() throws NumberFormatException. This parses text rather than casting a numeric object. A null object also cannot provide text through toString().
Conclusion
Choose the conversion based on what the Object actually contains: cast and unbox an Integer, use Number.intValue() for another numeric wrapper, or use Integer.parseInt() when its text represents an integer. When the object is known to contain an Integer, the direct cast is the simplest approach. Validate or handle the runtime type and nullability when those are not guaranteed.