Java Integer Methods

About This Index
This page is the Java Integer Methods index. Each row links to a full guide with syntax, parameters, return value, three runnable examples, and FAQs. Every example is a complete Java class with a main method and fixed data, and all of them compile on Java 8 and later.
New to Integer? It is the wrapper class for the primitive int, and it lives in java.lang, so you never import it. An int is a 32-bit whole number from Integer.MIN_VALUE (−2147483648) to Integer.MAX_VALUE (2147483647). Java boxes an int into an Integer automatically when a collection needs an object, and the class also holds helpers such as parseInt(), toString(), and compare().
Most of these methods are static: you call them on the class, as in Integer.parseInt("42"). A few are instance methods that work on an Integer object, as in count.intValue(). Each guide tells you which one it is. Also remember that an int wraps around silently when it overflows, so Integer.MAX_VALUE + 1 becomes Integer.MIN_VALUE.
How to Call an Integer Method
Call a static method on the class, and an instance method on a variable:
int age = Integer.parseInt("42"); // static: text to int
String bits = Integer.toBinaryString(age); // static: int to text, "101010"
int bigger = Integer.max(age, 100); // static: 100
Integer boxed = Integer.valueOf(age); // static: int to Integer
long wide = boxed.longValue(); // instance: 42
int order = boxed.compareTo(50); // instance: -1 Java Integer Methods Table
All method guides in this Java tutorial series (37 tutorials), grouped by purpose.
Text to number
5 methodsTurn a String into an int or an Integer: plain decimal numbers, other bases, hex and octal prefixes, unsigned values, and numbers stored in system properties.
| Method | Description | Tutorial |
|---|---|---|
parseInt() | Convert a String of digits into an int. | Try it |
parseUnsignedInt() | Read text as an unsigned 32-bit number. | Try it |
valueOf() | Get an Integer object from an int or a String. | Try it |
decode() | Parse text that may use a 0x, #, or 0 prefix. | Try it |
getInteger() | Read an int from a system property. | Try it |
Number to text
5 methodsTurn an int into a String in decimal, in any base from 2 to 36, or as raw binary, hex, and octal bits.
| Method | Description | Tutorial |
|---|---|---|
toString() | Convert an int to text, in base 10 or any base. | Try it |
toBinaryString() | Show the bits of an int as a binary String. | Try it |
toHexString() | Show the bits of an int in hexadecimal. | Try it |
toOctalString() | Show the bits of an int in octal (base 8). | Try it |
toUnsignedString() | Print an int as an unsigned decimal number. | Try it |
Compare and pick
8 methodsCompare two numbers, test equality, get a hash code, find the larger or smaller value, and read the sign of a number.
| Method | Description | Tutorial |
|---|---|---|
compare() | Compare two int values safely: negative, zero, or positive. | Try it |
compareTo() | Compare an Integer object with another Integer. | Try it |
compareUnsigned() | Compare two ints as unsigned 32-bit numbers. | Try it |
equals() | Check whether an Integer equals another object. | Try it |
hashCode() | Get the hash code of an Integer (its own value). | Try it |
max() | Return the larger of two int values. | Try it |
min() | Return the smaller of two int values. | Try it |
signum() | Get -1, 0, or 1 for a negative, zero, or positive number. | Try it |
Arithmetic and unsigned math
4 methodsAdd two ints as a method call, and treat the 32 bits of an int as an unsigned number for division, remainders, and widening to long.
| Method | Description | Tutorial |
|---|---|---|
sum() | Add two int values, as a method you can pass around. | Try it |
divideUnsigned() | Divide two ints as unsigned 32-bit numbers. | Try it |
remainderUnsigned() | Get the remainder of an unsigned division. | Try it |
toUnsignedLong() | Widen an int to a long as an unsigned value. | Try it |
Bit tricks
9 methodsCount bits, find the highest or lowest set bit, count leading and trailing zeros, reverse bits or bytes, and rotate bits left or right.
| Method | Description | Tutorial |
|---|---|---|
bitCount() | Count the 1 bits in the binary form of an int. | Try it |
highestOneBit() | Keep only the leftmost 1 bit of an int. | Try it |
lowestOneBit() | Keep only the rightmost 1 bit of an int. | Try it |
numberOfLeadingZeros() | Count the zero bits before the first 1 bit. | Try it |
numberOfTrailingZeros() | Count the zero bits after the last 1 bit. | Try it |
reverse() | Reverse the order of all 32 bits of an int. | Try it |
reverseBytes() | Reverse the order of the four bytes of an int. | Try it |
rotateLeft() | Rotate the bits of an int to the left in a circle. | Try it |
rotateRight() | Rotate the bits of an int to the right in a circle. | Try it |
Convert to other number types
6 methodsUnwrap an Integer to a primitive: int, long, double, float, short, or byte, and see what happens when the value does not fit.
| Method | Description | Tutorial |
|---|---|---|
intValue() | Unwrap an Integer into a primitive int. | Try it |
longValue() | Widen an Integer to a primitive long. | Try it |
doubleValue() | Convert an Integer to a primitive double. | Try it |
floatValue() | Convert an Integer to a primitive float. | Try it |
shortValue() | Narrow an Integer to a primitive short. | Try it |
byteValue() | Narrow an Integer to a primitive byte. | Try it |
Frequently Asked Questions
Start with bitCount()
Count the 1 bits in an int, the first guide in the list.
12 people found this page helpful
