Type Wrappers, Auto-boxing and Unboxing

Primitive types include int, double, ect are not objects (on the heap). When variables of primitive types are in scope, memory is allocated on the stack for them.

There are times when we want an object representation of primitive types. Each primitive type has a wrapper class associated with it.  These classes have fields, constructors and methods that can be very useful.

The type wrappers are: Double, Float, Long, Integer, Short, Byte, Character and Boolean.

int counter = 0;

Integer counter2 = new Integer(counter);   //wrapper

The Integer Class


  • static int BYTES                     // bytes used to represent an int
  • static int MAX_VALUE           // max value an int can have
  • static int MIN_VALUE            // min value an int can have


  • Integer(int value)
  • Integer(String s)        // throws exception if s does not contain a parsable string.


  • (47 of them)
  • Some interesting ones
    • boolean equals(Object obj)
    • static int compare(int x, int y)
    • static int parseInt(String s, int radix)
    • static int sum(int a, int b)
    • static int max(int a, int b)
    • static int min(int a, int b)
    • static String toString(int i, int radix);

AutoBoxing and Unboxing

A variable of a primitive wrapper type can be initialized with a primitive.  This is called auto-boxing.  When auto-boxing occurs, the system actually creates a new wrapper typed object automatically (line 2 below).  Similarly we can get the primitive value stored in a wrapper typed object using a simple assignment statement (line 3 below).  This is called unboxing.

int counter1 = 0;
Integer counter2 = counter1;          // auto-boxing
int counter3 = counter2;              // unboxing

© 2017, Eric. All rights reserved.