Primitive Wrapper Classes Tutorial

There are eight primitive data types in the Java language: byte, short, int, long, float, double, char, and boolean. Different rules apply to primitive types versus Object types and vice-versa. There are many occasions when we need to convert back a forth between object and primitive values and Java provides us the ability to do this via wrapper classes. What is a primitive wrapper class? The sole purpose of a wrapper class is to provide the ability to return a primitive data type from an object, or to provide an object from a primitive data type. The wrapper classes do this by using a combination of constructors and methods.
There is a wrapper class designed specifically for each and every one of the primitive data types: Byte, Short, Integer, Long, Float, Double, Character, and Boolean. Notice that the class name is identical – except the uppercase first letter – to the primitive data type on six of the eight classes, only Integer and Character differ in that respect. Each one of the eight wrapper classes has overloaded constructors and quite a few methods. I will be going into great detail on each individual wrapper class in future tutorials, but before I do so, I need to introduce you to the concept of boxing.
Boxing is the technical term for encapsulating a value within an object. Unboxing is the technical term for retrieving a value from an object. I'll demonstrate an example of a immutable class (all the primitive wrappers are immutable) that will box a primitive int into an object, then unbox it by invoking a method.
For example,
public final class IntWrapper {
        private final int intVal;
        IntWrapper(int intVal) {
                this.intVal = intVal;
        }
        public int getInt() {
               return intVal;
       }
}
Now we can make an object out of our new IntWrapper class and 'box' the primitive int value 41 ...
       int i = 41;
       IntWrapper iw = new IntWrapper( i );
// box the primitive int type value into the object
       i = iw.getInt();
// unbox the value from the wrapper object

My example IntWrapper class is immutable, immutable means that once its state has been initialized its state cannot be changed. The only state in my example class is the variable intVal and by declaring it final means that its value cannot be changed once it is initialized. Because I don't assign it a value when I declare it, the value must be assigned in a constructor – that ensures the variable can be initialized once and only once. I add the final modifier to the class and that prevents the class from being inherited or extended, in other words, my example class cannot be a superclass. If this paragraph does not make sense, then you may want to review my Final Primitive Data Type Variables Tutorial and my Final Classes Tutorial.



Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md PrimitiveWrappers
C:\Java>cd PrimitiveWrappers
C:\Java\PrimitiveWrappers>Notepad PrimitiveWrappers.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


class PrimitiveWrappers {
    public static void main(String args[]) {
        int i = 41;
        IntWrapper iw = new IntWrapper( i ); // box the primitive int type value into the object
        i = iw.getInt(); // unbox the int value from the wrapper object

        Integer ref = new Integer(i); // box the primitive int type value into the object
        i = ref.intValue(); // unbox the int value from the wrapper object
        System.out.println(i);
        String s = "i value: ".concat(ref.toString()); // unbox the value as a String object from the wrapper object
        System.out.println(s);
    }
}

final class IntWrapper {
    private final int intVal;

    IntWrapper(int intVal) {
        this.intVal = intVal;
    }

    public int getInt() {
        return intVal;
    }
}

Now switch back to the command prompt (CMD) and type in javac PrimitiveWrappers.java and press Enter.
Now type in java PrimitiveWrappers and press Enter.


C:\Java\PrimitiveWrappers>javac PrimitiveWrappers.java
C:\Java\PrimitiveWrappers>java PrimitiveWrappers
41
i value: 41


Final thoughts

The next two tutorials are going to tie in closely to this tutorial. The Integer Class tutorial and the Autoboxing and Autounboxing Tutorial will introduce you many of the seemingly magical things that Java does for us without us even knowing.


Tutorials