How to Compare an Integer with an int in Java (Unboxing)
When a Java comparison has an int on one side and an Integer object on the other, Java unboxes the Integer to an int and compares the two numbers. With an Integer on both sides, == compares the objects instead, and an Integer that is null stops the program with a NullPointerException as soon as Java tries to unbox it.
Syntax
primitive == wrapper
wrapper.equals(otherWrapper)
| Part | What it does |
|---|---|
primitive == wrapper | Compares an int with an Integer. Java unboxes wrapper to an int, so == compares the numbers. !=, <, <=, > and >= compare the numbers too. |
wrapper.equals(otherWrapper) | Compares the numbers inside two Integer objects. Use it instead of == when both sides are Integer. |
primitive | An int value or variable. |
wrapper, otherWrapper | Integer objects. Unboxing one that is null throws a NullPointerException. |
The Java Tutorials, Autoboxing and Unboxing: "Converting an object of a wrapper type (Integer) to its corresponding primitive (int) value is called unboxing." The Java Language Specification says when == does it: "If the operands of an equality operator are both of numeric type, or one is of numeric type and the other is convertible (§5.1.8) to numeric type, binary numeric promotion is performed on the operands" (§15.21.1), and "binary numeric promotion may include unboxing conversion".
The Example
The example compares an order amount of 120, an int, with a limit of 500, an Integer, using < and ==. Then it compares two Integer objects that both hold 1000, with ==, with equals, and with the int 1000. A changed version sets the limit to null.
Prerequisites
- A JDK 11 or later, which can run a single
.javafile directly. This guide uses OpenJDK 25.0.5.1.
Step 1: Write the Code
Create a file named Main.java:

public class Main {
public static void main(String[] args) {
Integer limit = 500; // an Integer object
int amount = 120; // a plain int
// One side is an int, so Java unboxes limit to an int and compares the numbers.
System.out.println(amount < limit);
System.out.println(amount == limit);
Integer a = 1000;
Integer b = 1000;
// Both sides are Integer objects, so == asks whether they're the same object.
System.out.println(a == b);
System.out.println(a.equals(b));
System.out.println(a == 1000);
}
}
Step 2: Run It
java Main.java
true
false
false
true
true
java Main.java compiles the file in memory and runs it. The command is the same in zsh, bash and PowerShell.
Step 3: Compare with a null Integer
Replace Main.java with a version whose limit is null:
public class Main {
public static void main(String[] args) {
Integer limit = null; // no limit was set
int amount = 120;
System.out.println(amount < limit);
}
}
java Main.java
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "<local1>" is null
at Main.main(Main.java:7)

What Happened
amount < limitistrue. Java unboxedlimitto theint500 and compared 120 with it.amount == limitisfalse. With aninton one side,==compared the numbers too, and 120 isn't 500.a == bisfalse, although both hold 1000. Both sides areIntegerobjects, so==asked whether they're the same object, and they're two objects. The specification: "At run time, the result of == is true if the operand values are both null or both refer to the same object or array; otherwise, the result is false."a.equals(b)istrue, becauseequalscompares the numbers.a == 1000istrue. Theint1000 on the right made Java unboxaand compare numbers.- With a
nulllimit, the program stopped. To compare, Java had to unboxlimitby calling itsintValue()method, andlimitwasnull. The specification: "If r is null, unboxing conversion throws a NullPointerException".
The message says <local1> instead of limit, because java Main.java compiles without the names of local variables. Compiled with javac -g instead, the message named the variable: because "limit" is null.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
== between two Integer objects | true for two objects holding 100, but false for two holding 1000 | Use equals, or compare with an int on one side |
Comparing an Integer that may be null with an int | NullPointerException: Cannot invoke "java.lang.Integer.intValue()" | Check for null first: limit != null && amount < limit was false for a null limit, with no exception |
equals between a Long and an Integer | false, even when both hold 500, because a Long never equals an Integer | Compare each with a primitive number: big == 500 was true for the Long 500 |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "<local1>" is null | An Integer that is null was unboxed for a comparison | Check it for null before you compare |
error: incomparable types: Long and Integer | == between a Long object and an Integer object | Compare each with a primitive number, or keep both values in the same type |
Two Integer objects with the same number aren't == | == compared the objects, not the numbers | Use equals |
FAQ
Why is == true for two Integer objects holding 100, but false for 1000?
Boxing reuses objects for small numbers. The Java Language Specification guarantees it for int values from -128 to 127, and adds: "For other values, the rule disallows any assumptions about the identity of the boxed values on the programmer's part." Two Integer objects holding 100 were ==, and two holding 1000 weren't. Don't rely on either: use equals.
What's the difference between int and Integer?
int is a primitive type that holds a number and can't be null. Integer is a class whose objects hold an int, and an Integer variable can be null. Collections such as List<Integer> and Map<String, Integer> hold Integer objects, so values that come out of them need these rules.
Conclusion
With an int on one side, Java unboxes the Integer and compares numbers, with == as well as <. With Integer objects on both sides, == compares the objects, so use equals. And check an Integer for null before comparing it: unboxing null throws a NullPointerException.
Additional Resources
- Autoboxing and Unboxing, in The Java Tutorials.
- Boxing Conversion and Unboxing Conversion, in the Java Language Specification.
- Numerical Equality Operators == and != and Reference Equality Operators == and !=, in the Java Language Specification.
