Home Java Integer Methods intValue() Java Integer intValue() Method Definition In Java, boxed.intValue() returns the plain int stored inside an Integer object. This is called unboxing . Java normally does it for you, so you rarely need to call the method by hand.
The method comes from the Number class, the parent of Integer, Long, Double, and the other wrapper types. That is why intValue() also works on a Double, where it cuts off the decimal part.
An instance method of java.lang.Integer: call it on an Integer object. Integer lives in java.lang, so no import is needed.
Syntax Call it on an Integer (or any Number). It takes no arguments.
Integer boxed = 250;
int a = boxed.intValue(); // 250
int b = boxed; // 250 (automatic unboxing)
Number n = 3.99;
int c = n.intValue(); // 3 (the decimal part is dropped) Call What it does boxed.intValue()The int inside an Integer. int n = boxed;Automatic unboxing; calls intValue() for you. number.intValue()Converts any Number to int, dropping any decimal part. Integer.valueOf(n)The opposite direction: boxes an int.
Calling intValue() (or letting Java unbox) on a null reference throws NullPointerException.
Library declarations // java.lang.Integer
public int intValue()
// Inherited contract from java.lang.Number
public abstract int intValue() Parameters Parameter Description None intValue() takes no parameters.
Return Value Return Value Description intThe primitive value held by the Integer. For other Number types it is a converted value, so a long or double may be truncated.
Important
The most common surprise is a NullPointerException: map.get("key") returns null for a missing key, and assigning that to an int unboxes the null. On other Number types, intValue() never rounds. 3.99 becomes 3 and -3.99 becomes -3, and a long that is too big for an int silently loses its upper bits.
Compare with Similar Methods Related conversions:
Method How it differs longValue()Unwraps to a wider long, so nothing is lost. doubleValue()Unwraps to a double, useful for division with decimals. valueOf(i)The reverse: wraps an int in an Integer. parseInt(s)Gets an int from text rather than from an object.
Java Examples 1. Unbox by hand and by itself 2. Other Number types 3. The null trap Example 1 — Unwrap an Integer into an int Unwrap an Integer explicitly, and compare with automatic unboxing in arithmetic.
public class IntegerIntValueDemo {
public static void main(String[] args) {
Integer boxed = 250;
int manual = boxed.intValue();
int automatic = boxed;
System.out.println("manual: " + manual);
System.out.println("automatic: " + automatic);
System.out.println("boxed + 5: " + (boxed + 5));
System.out.println("equal: " + (manual == automatic));
}
} manual: 250
automatic: 250
boxed + 5: 255
equal: true How It Works An object holds the number. boxed is an Integer object that stores 250.Two ways to unwrap. intValue() and plain assignment to an int both produce 250.Arithmetic unboxes too. The + operator unwraps the object by itself, so boxed + 5 is 255.Same result. The two int variables hold the same number, so the comparison is true.Example 2 — Convert Other Number Types with intValue Call intValue() on an array of mixed Number objects and see how decimals are handled.
public class IntegerIntValueNumber {
public static void main(String[] args) {
Number[] values = {Integer.valueOf(42), 3.99, -3.99, 7L, 2.5f, 3000000000L};
for (Number value : values) {
System.out.println(value + " -> " + value.intValue());
}
}
} 42 -> 42
3.99 -> 3
-3.99 -> -3
7 -> 7
2.5 -> 2
3000000000 -> -1294967296 How It Works One method, many types. The array holds Integer, Double, Long, and Float objects, and every one of them has intValue() from Number.Decimals are cut off. 3.99 becomes 3 and 2.5 becomes 2. Nothing is rounded.Negative decimals go toward zero. -3.99 becomes -3, not -4.A long that is too big. 3000000000 does not fit in an int, so only its low 32 bits remain and the result is -1294967296.Example 3 — Avoid the NullPointerException from Unboxing Store counts in a map and see what happens when you unbox a missing entry.
import java.util.HashMap;
import java.util.Map;
public class IntegerIntValueNull {
public static void main(String[] args) {
Map<String, Integer> stock = new HashMap<>();
stock.put("pens", 12);
int pens = stock.get("pens");
System.out.println("pens: " + pens);
Integer pencils = stock.get("pencils");
System.out.println("pencils is null: " + (pencils == null));
try {
int count = stock.get("pencils");
System.out.println(count);
} catch (NullPointerException e) {
System.out.println("unboxing null -> NullPointerException");
}
int safe = stock.containsKey("pencils") ? stock.get("pencils") : 0;
System.out.println("safe count: " + safe);
}
} pens: 12
pencils is null: true
unboxing null -> NullPointerException
safe count: 0 How It Works A present key unboxes fine. stock.get("pens") returns an Integer holding 12, and the assignment turns it into an int.A missing key gives null. Map.get returns null for "pencils", which is fine while it stays an Integer.Unboxing null fails. Assigning that null to an int calls intValue() on nothing, which throws NullPointerException.The safe pattern. Check containsKey first, or test for null, and use a default such as 0.Frequently Asked Questions What does Integer.intValue() do? It returns the primitive int stored inside an Integer object. It is the explicit form of automatic unboxing.
Do I need to call intValue() myself? Usually not. Java unboxes automatically when you assign an Integer to an int or use it in arithmetic.
Why do I get NullPointerException when I unbox an Integer? The Integer variable is null, so there is no object to unwrap. This often happens with Map.get for a missing key. Check for null first.
Does intValue() round decimals on a Double? No. Number.intValue() cuts the decimal part off, so 3.99 becomes 3 and -3.99 becomes -3. Use Math.round if you want rounding.
What happens when a Long is too big for an int? Long.intValue() keeps only the low 32 bits, so the result can be completely different. Use Math.toIntExact to get an exception instead.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
11 people found this page helpful
Helpful Share Copy link Suggestion