Java Integer Methods
The Integer class is the wrapper class for the int type. Its methods convert numbers to text and back, compare numbers, and write numbers in other number systems, like binary.
Most of the methods are static (they belong to the class), so you call them on the class name, for example Integer.parseInt("25"). You call the compareTo(), equals() and intValue() methods on an Integer object.
All Integer Methods
| Method | Description | Return Type |
|---|---|---|
| compare() | Compares two int values and returns a number that shows which one is larger | int |
| compareTo() | Compares this Integer with another Integer and returns a number that shows which one is larger | int |
| equals() | Checks if this Integer has the same value as another object | boolean |
| intValue() | Returns the value of this Integer as an int | int |
| max() | Returns the larger of two int values | int |
| min() | Returns the smaller of two int values | int |
| parseInt() | Converts a string to an int | int |
| sum() | Returns the sum of two int values | int |
| toBinaryString() | Returns a string with the number written in binary (base 2) | String |
| toHexString() | Returns a string with the number written in hexadecimal (base 16) | String |
| toOctalString() | Returns a string with the number written in octal (base 8) | String |
| toString() | Converts a number to a String | String |
| valueOf() | Returns an Integer object that holds the given int, or the number in a string | Integer |
Integer Fields
The Integer class also has two constants (fixed values) that hold the smallest and the largest value an int can have:
| Field | Description | Type |
|---|---|---|
| MAX_VALUE | The largest value an int can have (2147483647) | int |
| MIN_VALUE | The smallest value an int can have (-2147483648) | int |