Skip to main content

16. Wrapper Classes

 

Wrapper Classes

  • There is a wrapper class for every primitive in Java.
  • They provide a mechanism to wrap primitives so that they can be handles like objects.
  • They also provide an assortment of utility functions for primitives. Most of these functions are related to various conversions: converting primitives to and from String objects.
  • Wrapper objects are immutable. Once they have been given a value, that value cannot be changed (however reference can be changed).
  • class ShellClass {
        public static void main(String[] args) {
            Integer i = new Integer(1);
            System.out.println("Before modify(): " + i);
            modify(1);
            System.out.println("After  modify(): " + i);
        }

        static void modify(Integer i) {
            i++;
            System.out.println("During modify(): " + i);
        }
    }

The Wrapper Constructors

  • The following are the wrapper classes in the Java API:
  • Primitive Wrapper Class Constructor Arguments
    boolean Boolean boolean or String
    byte Byte byte or String
    char Character char
    double Double double or String
    float Float float, double or String
    int Integer int or String
    long Long long or String
    short Short short or String

    All these constructors are now depricated (from version 9 and above).

  • Those that take a String throw NumberFormatException if the String provided cannot be parsed into the appropriate primitive.
  • class ShellClass {
        public static void main(String[] args) {
            Integer in = new Integer("two");
        }
    }

  • If the String's case-insensitive value is "true" the Boolean will be true - any other value will equate to false.
  • class ShellClass {
        public static void main(String[] args) {
            Boolean b1 = new Boolean("True");
            Boolean b2 = new Boolean("TRUE");
            Boolean b3 = new Boolean("1");
            System.out.println("b1: " + b1 + "\nb2: " + b2 + "\nb3: " + b3);
        }
    }

  • As of Java 5, a Boolean object can be used in a boolean test, because the compiler will automatically "unbox" the Boolean to a boolean.
  • class ShellClass {
        public static void main(String[] args) {
            Boolean b = new Boolean("true");
            if (b)
                System.out.println("Boolean Test Passed...");
        }
    }

xxxValue() Methods

When we need to convert the value of a wrapped numeric to primitive, use one of the many xxxValue() methods.

class ShellClass {
    public static void main(String[] args) {
        Integer i = new Integer(69);
        byte b = i.byteValue();
        short s = i.shortValue();
        double d = i.doubleValue();
        System.out.println("Integer: " + i + "\nbyte: " + b + "\nshort: " + s + "\ndouble: " + d);
    }
}

parseXxx() and valueOf() Methods

parseXxx() returns the named primitive of the type that invoked the method i.e., Integer.parseInt(String) will return an int value.

class ShellClass {
    public static void main(String[] args) {
        double d = Double.parseDouble("3.14");
        long l = Long.parseLong("101010", 2);
        int i = Integer.parseInt("101010", 8);

        System.out.println("double: " + d + "\nlong: " + l + "\nint: " + i);
    }
}

valueOf() returns a newly created wrapped object of the type that invoked the method i.e., Integer.valueOf(String) returns a new Integer object.

class ShellClass {
    public static void main(String[] args) {
        Double d = Double.valueOf("3.14");
        Long l = Long.valueOf("101010", 2);
        Integer i = Integer.valueOf("101010", 8);

        System.out.println("Double: " + d + "\nLong: " + l + "\nInteger: " + i);
    }
}

toString() Method

Class Object has a toString() method. All of the wrapper classes have a no-arg, non-static, instance version of toString(). This method returns a String with the value of the primitive wrapped in the object - for instance.

class ShellClass {
    public static void main(String[] args) {
        Double d = new Double(3.14);
        System.out.println(d.toString());
    }
}

All of the wrapper classes provide an overloaded, static toString() method that takes a primitive numeric of the appropriate type and returns a String.

class ShellClass {
    public static void main(String[] args) {
        String d = Double.toString(3.14);
        System.out.println(d);
    }
}

Finally, Integer and Long provide another static toString() method whose first argument is the primitive, and its second argument is radix.

class ShellClass {
    public static void main(String[] args) {
        String s = "hex: " + Long.toString(69, 16);
        System.out.println(s);
    }
}

toXxxString() Methods

The Integer and Long wrapper classes let us convert numbers in base 10 to binary, hexadecimal and octal. These conversion methods, takes an int or long, and return a String representation of the converted number.

class ShellClass {
    public static void main(String[] args) {
        String s1 = Integer.toHexString(69);
        String s2 = Long.toOctalString(69);
        String s3 = Integer.toBinaryString(69);
        System.out.println("Hex: " + s1 + "\nOctal: " + s2 + "\nBinary: " + s3);
    }
}



Prev Next

Comments