Explaning Wrapper Classes in Java

Introduction

Variables declared using primitive data types are not considered objects. Java provides wrapper classes to manipulate primitive data types as objects. The java.lang package has a corresponding wrapper class for each of the primitive data types. By default, every Java program imports the java.lang package, and hence, there is no need to explicitly import this package into the program.

A wrapper class object can be constructed by passing a value to be wrapped into the corresponding constructor.

For Example

int num1=5;
Integer num = new Integer(num1) ;
int num2=num.intValue() ;

where num is the object of the wrapper class Integer. The last statement stores the value of num into num2 by accessing the method intValue ( ) through the wrapper class object num.

The wrapper classes in java.lang package is shown in the table below.

Wrapper classes

The example below how to make use of Wrapper classes.

class NumberWrap {
    protected NumberWrap() {
    }

    public static void main(final String[] args) {
        String number = args[0];
        Byte byNum = Byte.valueOf(number);
        Short shNum = Short.valueOf(number);
        Integer num = Integer.valueOf(number);
        Long lgNum = Long.valueOf(number);

        System.out.println("Output");
        System.out.println(byNum);
        System.out.println(shNum);
        System.out.println(num);
        System.out.println(lgNum);
    }
}

The example shows the valueOf( ) method is used to convert the corresponding primitive values to their Wrapper class counterparts. The program will throw a NumberFormatException if the number is greater than 127. On compiling the above example, the output will be generated as shown.

Number format

The various methods of the Character class.

Character class

The example below detects the type of characters using the methods of Character class.

class TestCharacter {
    protected TestCharacter() {
    }

    public static void main(final String[] args) {
        int count;
        char[] values = {'*', '7', 'q', ' ', 'Q'};

        for (count = 0; count < values.length; count++) {
            if (Character.isDigit(values[count])) {
                System.out.println(values[count] + " is a digit");
            }
            if (Character.isLetter(values[count])) {
                System.out.println(values[count] + " is a letter");
            }
            if (Character.isWhitespace(values[count])) {
                System.out.println(values[count] + " is whitespace");
            }
            if (Character.isUpperCase(values[count])) {
                System.out.println(values[count] + " is uppercase");
            }
            if (Character.isLowerCase(values[count])) {
                System.out.println(values[count] + " is lowercase");
            }
            if (Character.isUnicodeIdentifierStart(values[count])) {
                System.out.println(values[count] + " is valid first character of Unicode identifier");
            }
        }
    }
}

The program in the Example above initializes a character array with values. Then it makes use of the methods of character class to detect the type of each element in the array. For example, the following code.

if (Character.isLetter(values[count])) {
    System.out.println(values[count] + " is a letter");
}

determines if the particular element is a letter. The output is shown below.

Output

Summary

In Java, primitive data types (such as int, char, and boolean) are not treated as objects. To allow manipulation of these types as objects, Java provides wrapper classes in the java.lang package. Each primitive type has a corresponding wrapper class, like Integer for int and Boolean for boolean. Wrapper classes are essential for using primitives in object-required contexts, such as Collections. They allow the creation of objects by passing a primitive value to their constructors. Since the java.lang package is automatically imported. There's no need for explicit imports in Java programs.


Similar Articles