Skip to main content

Wrapper Example in Java

2 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Wrapper is a classic Java console program that demonstrates the concept with complete source code and sample output. The Collections Framework provides ArrayList, HashMap, HashSet and related data structures.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

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

When to use

Use this wrapper example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. int primitiveInteger = 500; updates a variable used in the calculation or output.

  3. A println / print call writes text to the console — part of the sample output below.

  4. Integer wrapperInteger = Integer.valueOf(primitiveInteger); updates a variable used in the calculation or output.

  5. A println / print call writes text to the console — part of the sample output below.

  6. primitiveInteger = wrapperInteger.intValue(); updates a variable used in the calculation or output.

  7. A println / print call writes text to the console — part of the sample output below.

  8. A println / print call writes text to the console — part of the sample output below.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Wrapper program demonstrate?
It shows how to implement wrapper in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials