Title: More Classes 2: Wrapper Classes
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

More Classes 2: Wrapper Classes

Wrapper Classes

1. Integer, Double, Float, Long, Character, Byte Wrapper classes exist. Note that Integer is associated with the int primitive and Character is associated with char primitive.
2. Wrapper classes provide useful static methods for dealing with the contained items.
3. Also there are times we need a class instead of a primitive to work with some of the more sophisticated Java classes. For example, Java provides an extensive package of data structure classes. These classes work with any type of class, but they don't work with primitives. Consequently if you want to store a bunch of int's, you end up converting them to the equivalent Wrapper class which is Integer.

//Don't worry if the following doesn't make a lot of sense now:
ArrayList<Integer> arrList = new ArrayList<Integer>();

4. Wrapper Classes are immutable. This means that once they are constructed, they can't be changed. The other important class that is immutable is the String class. My understanding is that the Wrapper classes and the String class are more efficient because they are immutable. My understanding is that the Wrapper classes are implemented in a "light-weight, and low overhead" fashion.
5. Wrapper classes don't have default contructors, and they do not have methods like setXXX() that could modify the contents of the class.
6. Boxing is the process of taking a primitive and creating an equivalent Wrapper class for it.


public class Wrappers {

    public static void main(String[] args) {
        // Pre Java 1.5 approach (still can be used)
        Integer numberOfSamuri = new Integer(47);
        Double price = new Double(499.99);
        Character grade = new Character('A');

        // As of Java 1.5 automatic boxing occurs with the following statements:
        Integer numberOfSamuri2 = 47;
        Double price2 = 499.99;
        Character grade2 = 'A';

    }

}
	

7. UnBoxing is the process of obtaining the underlying primitive value that is equivalent to a particular Wrapper class.


public class Wrappers {

    public static void main(String[] args) {
        Integer numberOfSamuri = new Integer(47);
        int n = numberOfSamuri.intValue(); // Pre 1.5 java approach
        int n2 = numberOfSamuri; // Java 1.5 and later - automatic unboxing

        Double price = new Double(499.99);
        double d = price.doubleValue(); // Pre 1.5 java approach
        double d2 = price; // Java 1.5 and later - automatic unboxing

        Character grade = new Character('A');
        char c = grade.charValue(); // Pre 1.5 java approach
        char c2 = grade; // Java 1.5 and later - automatic unboxing
    }

}
	

8. Useful static methods in Number Wrapper classes
static double Double.parseDouble(String str) - recommend calling trim on string
static int Integer.parseInt(String str)
static long Long.parseLong(String str)


    public class NumberWrapperMethods {

    public static void main(String[] args) {

        double d = Double.parseDouble("1.234");
        int x = Integer.parseInt("123");
        long lx = Long.parseLong("12345");

        int  h = Integer.parseInt("f2", 16);
        int  binary = Integer.parseInt("1011", 2);
        System.out.println("h="+ h + " binary="+binary);

        //These routines are very picky.  The following are commented out
        // because you will get an NumberFormatException

        //int e = Integer.parseInt(" 123");
        //int e2 = Integer.parseInt("123 ");
        //int e3 = Integer.parseInt("123.");

    }

}



9. Userful Methods in the Character Wrapper class
static char Character.toUpperCase(char c)
static char Character.toLowerCase(char c)
static boolean Character.isUpperCase(char c)
static boolean Character.isWhitespace(char c)
static boolean Character.isLetter(char c)
static boolean Character.isDigit(char c)
static boolean Character.isLetterOrDigit(char c)


public class CharacterWrapperTest {

    public static void main(String[] args) {
        String s="Ab9& \t\n";

        for (int i=0; i < s.length(); i++)
        {
            char c = s.charAt(i);

            System.out.println("*********  c =" + c + "   **********");
            if (Character.isUpperCase(c))  System.out.println("Upper Case");
            if (Character.isLowerCase(c))  System.out.println("Lower Case");
            if (Character.isWhitespace(c))  System.out.println("is White space");
            if (Character.isLetter(c))  System.out.println("is Letter");
            if (Character.isDigit(c))  System.out.println("is Digit");
            if (Character.isLetterOrDigit(c))  System.out.println("is Letter or Digit");

            System.out.println("Upper Cased=" + Character.toUpperCase(c));
            System.out.println("Lower Cased=" + Character.toLowerCase(c));
        }

    }

}


Java Practice(JPHO): (5 points)

public class Test {

    public static void main(String[] args) {
        String si1="123";
        String si2=" 123";
        String si3="123.1";

        // What happens if you use Integer.parseInt to convert si1, si2, and si3 to integers?

        String sd1="123";
        String sd2="123.456";
        String sd3="d123.456";

        // What happens if you use Double.parseDouble to convert sd1, sd2, and sd3 to doubles?
    }
}

Last Modified: December 23, 2014 9:26 AM