Wrapper Example Java Program


Definition

A wrapper class is a class that encapsulates types so that those types can be used to create object instances and methods in another class that need those types.

Syntax

Integer variable-name = new Integer(10);

Wrapper Example Program

public class WrapperExample {

    public static void main(String[] args) {
        int primitiveInteger = 500;
        System.out.println("Primitive Integer : " + primitiveInteger);
        Integer wrapperInteger = Integer.valueOf(primitiveInteger);
        System.out.println("After assigning this value in Wrapper Integer : " + wrapperInteger);
        primitiveInteger = wrapperInteger.intValue();
        System.out.println("Again assigning this value in Primitive Integer : " + primitiveInteger);
        String integerString = "1000";
        System.out.println("String to be converted to integer : " + integerString);
        System.out.println("Converting String to integer : " + Integer.parseInt(integerString));
    }
}

Sample Output

Primitive Integer : 500
After assigning this value in Wrapper Integer : 500
Again assigning this value in Primitive Integer : 500
String to be converted to integer : 1000
Converting String to integer : 1000