Java Reference ยท java.lang
The Integer class
Reference for java.lang.Integer: the int wrapper, constants, parsing, conversion, bit operations, caching pitfalls with ==, unsigned helpers and null handling.
Integer is the wrapper class for the primitive int. It lets an int live where an object is required: in a List<Integer>, as a Map key, or as a generic type argument. Java converts between int and Integer automatically (boxing and unboxing), which is convenient but hides some traps.
Beyond wrapping, the class is a toolbox of static methods for parsing text, formatting numbers in other bases, comparing, and bit manipulation. It extends Number and implements Comparable<Integer>. Instances are immutable.
Integer boxed = 42; // autoboxing int primitive = boxed; // unboxing int n = Integer.parseInt("123");
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.
Syntax
public final class Integer extends Number implements Comparable<Integer>
| Member | Description |
|---|---|
MAX_VALUE, MIN_VALUE |
2147483647 and -2147483648. |
SIZE, BYTES |
32 and 4. |
Integer.valueOf(int), valueOf(String) |
Returns an Integer; values from -128 to 127 are cached. |
parseInt(String), parseInt(String, int radix) |
Text to int (see parseInt()). |
toString(int), toBinaryString, toHexString, toOctalString |
Conversion to text (see toString()). |
compare(int, int), compareTo(Integer) |
Ordering (see compare()). |
sum, max, min |
Method references for streams: reduce(0, Integer::sum). |
bitCount, reverse, rotateLeft, highestOneBit, numberOfLeadingZeros |
Bit tricks. |
parseUnsignedInt, toUnsignedString, divideUnsigned, remainderUnsigned |
Unsigned interpretation of the 32 bits. |
intValue(), longValue(), doubleValue() |
Instance conversions inherited from Number. |
Integer.getInteger(String), decode(String) |
System-property lookup, and parsing of 0x/0 prefixes. |
Throws: NumberFormatException for unparseable text; NullPointerException when you unbox null.
Usage notes
- Use
equals(), not==, onIntegerobjects.valueOfcaches -128..127 only, so==on larger values compares identity and is usuallyfalse. Comparing anIntegerwith anintusing==unboxes and is fine. - Unboxing
nullthrows. AMap<String, Integer>lookup that misses returnsnull, and assigning it to anintthrows aNullPointerException. UsegetOrDefault(). new Integer(...)is deprecated for removal. UseInteger.valueOf()or autoboxing. Do not rely on object identity of wrappers.- Performance. Boxing allocates. Prefer primitive arrays,
IntStreamand primitive-specialized collections in hot code. - Overflow wraps. Arithmetic on
inthas no overflow check; seeMath.addExact(). - Lists of
Integer. On aList<Integer>,list.remove(1)removes by index andlist.remove(Integer.valueOf(1))removes by value. - Unsigned. Java has no unsigned
inttype, but the*Unsignedmethods reinterpret the same bits. Integer.MIN_VALUEhas no positive counterpart:-MIN_VALUE,abs(MIN_VALUE)andMIN_VALUE / -1all stay negative or overflow.