Wrapper Classes

by Michael Thomas

(Tutorial Home Page)

by Michael Thomas

(Tutorial Home Page)

(under construction)

Objectives (topics)

Topic Notes
Wrapper classes A wrapper is a class that contains data or an object of a specific type and has the ability of performing special operations on the data or object.   Most common wrapper classes are for the primitives.

Reasons for wrapper around the primitives:

  • Converting from character strings to numbers and then to other primitive data types.
  • A way to store primitives in an object.

Primitive and the Wrapper Class:  

  • Integer Primitives
    • byte - Byte
    • short - Short
    • int - Integer
    • long - Long
  • Floating Point Primitives
    • float - Float
    • double - Double;
  • Other Wrappers for Primitives
    • char - Character
    • boolean - Boolean;

ex:  Double MyDouble = new Double("10.5");

Examples Converting Strings to Primitives
  • strTest = "10"; int intMy = Integer.parseInt( strTest );
  • strTest = "10.5"; double dblMy = Double.parseDouble( strTest );

Converting Primitives to Strings

  • intMy = 10; String strTest = Integer.toString( intMy );
  • dblMy = 10.0; String strTest = Double.toString( dblMy );

Instantiating Wrapper Classes

  • Integer Class
    • strTest = "10"; Integer MyInteger = new Integer(strTest);
    • intTest = 10; Integer MyInteger = new Integer(intTest);
  • Double Class
    • strTest = "10.5"; Double MyDouble = new Double(strTest);
    • dblTest = 10.5; Double MyDouble = new Double(dblTest);