Skip to main content

17. Autoboxing

 

Autoboxing

Boxing and unboxing make using wrapper classes more convenient. in the old, pre-Java-5 days, if we wanted to make wrapper, unwrap it, use it, and then rewrap it.

class ShellClass {
    public static void main(String[] args) {
        Integer i = new Integer(1);
        int x = i.intValue(); // unwrap or unboxing
        x++;
        i = new Integer(x); // rewrap or boxing
        System.out.println(i);
    }
}

Now, with new and improved Java 5

class ShellClass {
    public static void main(String[] args) {
        Integer i = new Integer(1);
        i++; // Autoboxing
        System.out.println(i);
    }
}

Behind the scenes, the compiler does the unboxing and reassignment for us. Wrapper objects are immutable, but this example appears to contradict this statement. But actually, a second wrapper object was created.

class ShellClass {
    public static void main(String[] args) {
        Integer y = new Integer(1);
        Integer x = y;

        System.out.println(x == y);
        y++;
        System.out.println(x == y);
    }
}

Boxing, == and equals()

The API developers decided that for all the wrapper classes, two objects are equal if they are of the same type and have the same value.

class ShellClass {
    public static void main(String[] args) {
        Integer x1 = 1000;
        Integer y1 = 1000;
        System.out.print("x1 == y1: " + (x1 == y1));
        System.out.println("\tx1 != y1: " + (x1 != y1));

        Integer x2 = 10;
        Integer y2 = 10;
        System.out.print("x2 == y2: " + (x2 == y2));
        System.out.println("\tx2 != y2: " + (x2 != y2));
    }
}

In order to save memory, two instances of the following wrapper objects (created through boxing), will always be == when their primitive values are the same:

  • Boolean
  • Byte
  • Character from \u0000 to \u007f (7f is 127 in decimal)
  • Short and Integer from -28 to 127

Creating wrapper objects using new keyword with same primitive value will also create different objects.

class ShellClass {
    public static void main(String[] args) {
        Integer a = new Integer(10);
        Integer b = new Integer(10);
        System.out.print("a == b: " + (a == b));
        System.out.println("\ta != b: " + (a != b));

        Integer c = new Integer(1000);
        Integer d = new Integer(1000);
        System.out.print("c == d: " + (c == d));
        System.out.println("\tc != d: " + (c != d));
    }
}

When == is used to compare a primitive to a wrapper, the wrapper will be unwrapped and the comparison will be primitive to primitive.

class ShellClass {
    public static void main(String[] args) {
        int a = 10;
        Integer b = new Integer(10);
        System.out.print("a == b: " + (a == b));
        System.out.println("\ta != b: " + (a != b));

        int c = 1000;
        Integer d = new Integer(1000);
        System.out.print("c == d: " + (c == d));
        System.out.println("\tc != d: " + (c != d));
    }
}



Prev Next

Comments