University of Cincinnati logo and link  
ArrayLists
 

 

Storing Numbers in ArrayLists

 
Remember the differences between primitives and objects?  ArrayLists can only store Objects.  If we want to store a primitive, we have to wrap it in a wrapper class.  A wrapper class is essentially an object that stores, or 'wraps', a primitive value so that it can be treated more like an object.

There are wrappers for the primitives.
Most wrappers have a method that starts with parse to turn a String into a primitive of that type.  They also have a method ending in Value that returns the primitive type.  To create a wrapper object for a given primitive type, pass the primitive, or a String representation of the primitive, into the constructor for the wrapper.