Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท Types and Literals

Wrapper classes

Reference for Java wrapper classes: Integer, Long, Double, Boolean, Character and friends, when they are needed, caching, nulls and useful static methods.

Every primitive type has a matching wrapper class in java.lang: Byte, Short, Integer, Long, Float, Double, Character and Boolean. A wrapper is an immutable object that holds one primitive value. You need them wherever an object is required: generic type arguments (List<Integer>, Map<String, Double>), Optional, nullable values and reflection.

The wrappers also host the useful static methods for their type: parsing, conversion to text, comparison and bit operations. Java converts between primitive and wrapper automatically; see autoboxing.

Java
Integer boxed = 42;
int primitive = boxed;                 // unboxing
Integer parsed = Integer.valueOf("17");

Try it

Java Wrappers in collections and as nullable values

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 Static helpers on every wrapper

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

Primitive Wrapper Parse Notable constants
byte Byte Byte.parseByte(s) MIN_VALUE, MAX_VALUE
short Short Short.parseShort(s) MIN_VALUE, MAX_VALUE
int Integer Integer.parseInt(s) MIN_VALUE, MAX_VALUE, SIZE, BYTES
long Long Long.parseLong(s) MIN_VALUE, MAX_VALUE
float Float Float.parseFloat(s) NaN, POSITIVE_INFINITY
double Double Double.parseDouble(s) NaN, POSITIVE_INFINITY
char Character none MIN_VALUE, MAX_VALUE
boolean Boolean Boolean.parseBoolean(s) TRUE, FALSE

Create wrappers with valueOf() or by autoboxing. The constructors (new Integer(5)) are deprecated for removal since Java 9 and should not be used.

Usage notes

  • Immutable. A wrapper never changes; operations return new values.
  • Compare with equals(), not ==. == compares references, and only small values (-128 to 127 for Integer, Long, Short, Byte, and Character up to 127) are guaranteed to be cached.
  • equals() checks the class too: Integer.valueOf(7).equals(7L) is false.
  • Null unboxing throws NullPointerException. Be careful with Map.get() results assigned to a primitive.
  • Cost. An Integer is an object with a header, and collections hold references. For large numeric data use arrays or primitive streams (IntStream) to avoid boxing.
  • Number hierarchy. Byte, Short, Integer, Long, Float and Double extend java.lang.Number; Character and Boolean do not.
  • Parse methods throw NumberFormatException on bad input (including the empty string and surrounding spaces); Boolean.parseBoolean() never throws.
  • Value-based classes. Do not synchronize on wrappers; their identity is not meaningful.