Java Integer equals() Method
Example
Check if Integer objects have the same value:
public class Main {
public static void main(String[] args) {
Integer a = 1000;
Integer b = 1000;
Integer c = 2000;
System.out.println(a.equals(b));
System.out.println(a.equals(c));
}
}
Definition and Usage
The equals() method checks if an Integer has the same value as another object. It returns true only if the other object is also an Integer with the same value.
Always use equals() to compare Integer objects. The == operator checks if two variables point to the same object, so it can return false even when the values are equal (often for numbers below -128 or above 127).
Syntax
public boolean equals(Object obj)
Parameter Values
| Parameter | Description |
|---|---|
| obj | Required. The object to compare with. |
Technical Details
| Returns: | true if obj is an Integer with the same value, otherwise false. |
|---|
Related Pages
❮ Integer Methods