The Wrapper Classes

  • A wrapper class is simply a class that encapsulates a single, immutable value.
  • All the wrapper classes can be constructed by passing the value to be wrapped into the appropriate constructor, most of these constructors throw NumberFormat-Exception.
  • The values wrapped inside two wrappers of the same type can be checked for equality by using the equals() method
  • The wrapper classes are useful whenever it would be convenient to treat a piece of primitive data as if it were an object. (Example: Vector)
  • Wrapped values can be extracted with various XXXValue() methods.All six numeric wrapper types support all six numeric XXXValue() methods.
  • After an instance of wrapper class is constructed, the encapsulated value cannot be changed.
  • Java’s wrapper classes are useful and provide a great deal of functionality, well beyond that of the primitive types.
  • Code using Wrapper classes and primitives can be convoluted. Use Java 1.5’s generics, autoboxing and the enhanced for loop to avoid unclear code.