Title: Wrapper Classes
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Wrapper Classes: Double, Float, Long, etc.

We have mentioned the wrapper classes before, but at this stage it's useful to mention some handy features of these classes.

1. The classes Double, Float, Long, Integer, Short, Byte, Character, and Boolean are thin wrapper classes that wrap the corresponding primitives: double, float, long, int, short, byte, char, and boolean respectively.
2. There are some useful methods (many of them static) that allow you to convert between strings and numbers. Also you can get conversion between different bases for example hex, binary, etc. In this section we will look at this aspect.
3. The other reason for the existence of these classes is the fact that there are a lot of classes that work with Java classes, but don't work with primitives. For example, there are Dynamically Growable Array classes, Linked Lists, Tree datastructures, etc that all work with Java classes (anything derived from Object). If you really want to store primitives into these structures you need to wrap the primitive with the corresponding Wrapper class. We will be covering this fact when we get to the Collections class discussion.

Looking at Number Wrapper Classes

A small subset of the Wrapper class methods. The examples below have been useful to me in the past.


class Wrappers{

  public static void main(String[] args)
  {
    String strInt = "123";
    String strHex = "1234beef";
    String strReal = "123.123";
    String strVal;
    int i;
    long l;
    double d;
    float f;
    short s;
    byte b;

    // Integer Class
    // This class has many useful static methods

    // Note that parseInt throws exceptions if you pass
    // bad strings (i.e. 123x4)---- we will cover exceptions later
    i = Integer.parseInt(strInt);
    i = Integer.parseInt(strHex, 16); // Converts Hex number to int value

    strVal = Integer.toBinaryString(123);
    System.out.println("123 in binary is : " + strVal);
    strVal = Integer.toHexString(123);
    System.out.println("123 in Hex is : " + strVal);
    strVal = Integer.toOctalString(123);
    System.out.println("123 in Octal is " + strVal);

    // Long Class
    // The long class has most of the same methods as the Integer class
    // the name parseInt is replaced with parseLong

    l = Long.parseLong("1234deadbeef", 16);
    // What happens in the following.  Uncomment it and find out.
    //! l = Integer.parseInt("1234deadbeef", 16);

    // Short Class and Byte class don't have as many features as
    // the Integer class and the Long class, but you at least have
    // parseShort and parseByte
    s = Short.parseShort(strInt);
    s = Short.parseShort("1ad9", 16);

    b = Byte.parseByte(strInt);
    b = Byte.parseByte("6f",16);

    // Double Class
    d = Double.parseDouble(strReal);

    // Float Class
    f = Float.parseFloat(strReal);

  }
}


Character Class

There are a lot more methods in the Character class, but this is just to give you the flavor of what is available.


class Wrapper2
{
  public static void main(String[] args)
  {
      Character Ch;
      char ch;

      for (ch =0; ch < 128; ch++)
      {
      // This is just a small sampling of the features of
      // the Character class.

        System.out.println(
            ch +
            " int: " + (int)ch +
            " type: " + Character.getType(ch) + nameType(ch) +
            " isLetterOrDigit: " + Character.isLetterOrDigit(ch)
            );
      }

  }
  static String nameType(char ch)
  {
    int type;
    type = Character.getType(ch);
    switch(type)
    {
    case Character.SPACE_SEPARATOR:
        return "SPACE_SEPARATOR";
    case Character.DECIMAL_DIGIT_NUMBER:
        return "DECIMAL_DIGIT_NUMBER";
    case Character.LOWERCASE_LETTER:
        return "LOWERCASE_LETTER";

    case Character.UPPERCASE_LETTER:
        return "UPPERCASE_LETTER";

    default:
        return "unknown";
    }
  }
}


Boolean Class

-- didn't find anything too exciting here

Last Updated: September 2, 2002 4:36 PM