Skip to content
Tutorials Center
My learning

Topics

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.

Java
Integer boxed = 42;           // autoboxing
int primitive = boxed;        // unboxing
int n = Integer.parseInt("123");

Try it

Java Constants, conversion and bits

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.

Java Boxing, caching and null

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

Java
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 ==, on Integer objects. valueOf caches -128..127 only, so == on larger values compares identity and is usually false. Comparing an Integer with an int using == unboxes and is fine.
  • Unboxing null throws. A Map<String, Integer> lookup that misses returns null, and assigning it to an int throws a NullPointerException. Use getOrDefault().
  • new Integer(...) is deprecated for removal. Use Integer.valueOf() or autoboxing. Do not rely on object identity of wrappers.
  • Performance. Boxing allocates. Prefer primitive arrays, IntStream and primitive-specialized collections in hot code.
  • Overflow wraps. Arithmetic on int has no overflow check; see Math.addExact().
  • Lists of Integer. On a List<Integer>, list.remove(1) removes by index and list.remove(Integer.valueOf(1)) removes by value.
  • Unsigned. Java has no unsigned int type, but the *Unsigned methods reinterpret the same bits.
  • Integer.MIN_VALUE has no positive counterpart: -MIN_VALUE, abs(MIN_VALUE) and MIN_VALUE / -1 all stay negative or overflow.