CodeToConcept
About

java

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

java Tutorials

Introduction
Java Variables
Java Data Types
Java Type Casting
Java Operators
Java Input/Output
Java Class & Object
java Method
Java Constructor
Java this keyword
Java super keyword
Java Method Overloading and Overriding
Java Interface
Java Abstract Class
Java Memory Management
Design Patterns
JDBC (Database Connectivity)
Java IDEs

Sample Question

Think it through, then check the tutorial.

CodeToConcept

Quickly learn through simple, structured, and practical coding tutorials. Build your confidence from step zero to real-world experience.

© 2026 CodeToConcept. All rights reserved.

Quick Links

AboutTutorials

Tutorials

JavaHTMLCSS

Support

Privacy PolicyTerms of ServiceContact
GitHubTwitterYouTube

Java Wrapper Classes

Wrapper classes represent primitive-style values as objects. They let values such as int numbers be used with APIs that work with references, including generic collections. Conversion, null handling, and equality need care because a wrapper is not the same thing as its primitive value.

Sometimes, however, we need to treat a primitive value as an object. Wrapper classes provide object representations of primitive data types.

For example:

int number = 10;

Here, number is a primitive value.

The corresponding wrapper class is:

Integer number = 10;

Here, Integer is an object type that represents the primitive int.

What Are Wrapper Classes?

A wrapper class is a class that wraps a primitive data type inside an object.

Java provides a wrapper class for each primitive type.

Primitive TypeWrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

There is no wrapper class named Void for the void primitive in the same sense, but Java provides java.lang.Void as a special class representing the void type.

All the standard wrapper classes are available in the java.lang package, so they do not need to be explicitly imported.

Why Do We Need Wrapper Classes?

Java has two broad categories of types:

Java Types
│
├── Primitive Types
│   ├── int
│   ├── char
│   ├── double
│   └── ...
│
└── Reference Types
    ├── Integer
    ├── Character
    ├── String
    └── ...

Primitive types are simple and efficient, but they are not objects.

Wrapper classes are useful when an object is required.

For example, collections such as ArrayList work with objects:

ArrayList<Integer> numbers;

We cannot write:

ArrayList<int> numbers;

because Java generics do not directly accept primitive types.

Therefore, wrapper classes allow primitive values to be used where objects are required.

Primitive vs Wrapper

Consider:

int a = 10;
Integer b = 10;

Here:

a → primitive int
b → Integer object/reference

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int a = 10;
        Integer b = 10;
 
        System.out.println(a);
        System.out.println(b);
    }
}

Output

10
10

The output is the same, but the types are different.

Wrapper Class Hierarchy

The wrapper classes are classes, so they can participate in Java's object-oriented features.

For example:

Object
  |
  +-- Number
       |
       +-- Byte
       +-- Short
       +-- Integer
       +-- Long
       +-- Float
       +-- Double

Character and Boolean do not extend Number.

Creating Wrapper Objects

There are several ways to obtain wrapper objects.

Using a value directly

Integer number = 100;

This commonly involves autoboxing, which is explained later.

Using valueOf()

Integer number = Integer.valueOf(100);

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer number = Integer.valueOf(100);
 
        System.out.println(number);
    }
}

Output

100

Using valueOf() is generally preferred over manually creating wrapper objects.

Deprecated Wrapper Constructors

Older Java code may contain:

Integer number = new Integer(100);

Modern Java code should avoid this style. Wrapper constructors such as new Integer(int) are deprecated for removal. They still exist in Java 25, but new code should use autoboxing or valueOf() instead.

Prefer:

Integer number = Integer.valueOf(100);

or:

Integer number = 100;

Autoboxing

Autoboxing is the automatic conversion of a primitive value into its corresponding wrapper object.

Example

int number = 10;
 
Integer wrapper = number;

Java automatically converts:

int → Integer

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int number = 10;
 
        Integer wrapper = number;
 
        System.out.println(wrapper);
    }
}

Output

10

Conceptually, Java performs something similar to:

Integer wrapper = Integer.valueOf(number);

Unboxing

Unboxing is the automatic conversion of a wrapper object back into its corresponding primitive type.

Example

Integer wrapper = 10;
 
int number = wrapper;

Java automatically converts:

Integer → int

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer wrapper = 10;
 
        int number = wrapper;
 
        System.out.println(number);
    }
}

Output

10

Conceptually, Java performs something similar to:

int number = wrapper.intValue();

Autoboxing and Unboxing Together

Both conversions can happen automatically.

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int a = 10;
 
        Integer b = a;  // Autoboxing
 
        int c = b;      // Unboxing
 
        System.out.println(c);
    }
}

Output

10

The flow is:

int
 ↓
Autoboxing
 ↓
Integer
 ↓
Unboxing
 ↓
int

Wrapper Classes with Collections

One of the most important uses of wrapper classes is with Java collections.

Consider:

ArrayList<Integer> numbers = new ArrayList<>();

We cannot write:

ArrayList<int> numbers = new ArrayList<>();

because int is a primitive type.

Example

import java.util.ArrayList;
 
public class HelloWorld {
 
    public static void main(String[] args) {
 
        ArrayList<Integer> numbers = new ArrayList<>();
 
        numbers.add(10);
        numbers.add(20);
        numbers.add(30);
 
        System.out.println(numbers);
    }
}

Output

[10, 20, 30]

The values are automatically boxed from int to Integer.

Wrapper Classes as Method Parameters

A method can accept a wrapper object.

Example

public class HelloWorld {
 
    static void display(Integer number) {
        System.out.println("Number: " + number);
    }
 
    public static void main(String[] args) {
 
        int value = 100;
 
        display(value);
    }
}

Output

Number: 100

The primitive int is automatically boxed into Integer.

Wrapper Classes as Return Types

A method can also return a wrapper object.

public class HelloWorld {
 
    static Integer getNumber() {
        return 100;
    }
 
    public static void main(String[] args) {
 
        Integer number = getNumber();
 
        System.out.println(number);
    }
}

Output

100

Converting String to Wrapper

Wrapper classes provide methods for converting strings into numeric wrapper values.

For example:

String value = "100";
 
Integer number = Integer.valueOf(value);

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        String value = "100";
 
        Integer number = Integer.valueOf(value);
 
        System.out.println(number);
    }
}

Output

100

parseInt() vs valueOf()

Integer provides both:

Integer.parseInt()

and:

Integer.valueOf()

The important difference is their return type.

parseInt()

Returns a primitive int.

int number = Integer.parseInt("100");

valueOf()

Returns an Integer.

Integer number = Integer.valueOf("100");

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int a = Integer.parseInt("100");
 
        Integer b = Integer.valueOf("200");
 
        System.out.println(a);
        System.out.println(b);
    }
}

Output

100
200

Parsing Other Numeric Types

Similar methods exist for other wrapper classes.

byte b = Byte.parseByte("10");
 
short s = Short.parseShort("20");
 
int i = Integer.parseInt("30");
 
long l = Long.parseLong("40");
 
float f = Float.parseFloat("50.5");
 
double d = Double.parseDouble("60.5");

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        int number = Integer.parseInt("123");
        double price = Double.parseDouble("99.50");
 
        System.out.println(number);
        System.out.println(price);
    }
}

Output

123
99.5

Converting Wrapper to String

Wrapper classes can be converted into strings.

Example

Integer number = 100;
 
String value = number.toString();

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer number = 100;
 
        String value = number.toString();
 
        System.out.println(value);
        System.out.println(value.getClass().getName());
    }
}

Output

100
java.lang.String

Integer.toString()

You can also convert an integer directly to a string:

String value = Integer.toString(100);

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        String value = Integer.toString(100);
 
        System.out.println(value);
    }
}

Output

100

Wrapper Class Constants

Wrapper classes provide useful constants.

For example:

System.out.println(Integer.MAX_VALUE);
System.out.println(Integer.MIN_VALUE);

Output

2147483647
-2147483648

Similarly:

System.out.println(Long.MAX_VALUE);
System.out.println(Double.MAX_VALUE);
System.out.println(Float.MAX_VALUE);

Wrapper classes provide constants describing the limits and characteristics of their corresponding types.

Integer.MAX_VALUE

The maximum value of an int is:

Integer.MAX_VALUE

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        System.out.println("Maximum int: " + Integer.MAX_VALUE);
        System.out.println("Minimum int: " + Integer.MIN_VALUE);
    }
}

Output

Maximum int: 2147483647
Minimum int: -2147483648

Wrapper Class Utility Methods

Wrapper classes provide useful methods for working with primitive values and strings.

For example, Integer provides:

Integer.parseInt()
Integer.valueOf()
Integer.toString()
Integer.compare()
Integer.max()
Integer.min()
Integer.sum()

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        System.out.println(Integer.max(10, 20));
        System.out.println(Integer.min(10, 20));
        System.out.println(Integer.sum(10, 20));
    }
}

Output

20
10
30

Comparing Wrapper Objects

This is an important topic.

Consider:

Integer a = 100;
Integer b = 100;
 
System.out.println(a == b);

The result may be:

true

But you should not use == to compare wrapper objects by value.

Use:

equals()

instead.

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer a = 100;
        Integer b = 100;
 
        System.out.println(a.equals(b));
    }
}

Output

true

Why Can == Give Unexpected Results?

Some wrapper classes cache commonly used values.

For Integer, values in a commonly cached range are typically reused, so:

Integer a = 100;
Integer b = 100;
 
System.out.println(a == b);

may print:

true

But:

Integer a = 1000;
Integer b = 1000;
 
System.out.println(a == b);

may print:

false

The exact behavior of wrapper caching should not be used as a value-comparison technique.

The correct approach is:

a.equals(b)

when both references are non-null.

Wrapper Comparison with compareTo()

Wrapper classes such as Integer implement Comparable.

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer a = 10;
        Integer b = 20;
 
        System.out.println(a.compareTo(b));
        System.out.println(b.compareTo(a));
        System.out.println(a.compareTo(10));
    }
}

Output

-1
1
0

The meaning is:

negative → first value is smaller
zero     → values are equal
positive → first value is greater

Null Values with Wrapper Classes

Primitive types cannot store null.

This is invalid:

int number = null;

But a wrapper reference can:

Integer number = null;

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer number = null;
 
        System.out.println(number);
    }
}

Output

null

This is one important difference between primitives and wrapper references.

NullPointerException During Unboxing

Be careful when unboxing a null wrapper.

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer number = null;
 
        int value = number;
 
        System.out.println(value);
    }
}

This causes a:

NullPointerException

Why?

Because Java effectively needs to call something equivalent to:

number.intValue()

But number is null.

Therefore, unboxing a null wrapper reference causes a NullPointerException.

Wrapper Classes and Method Overloading

Wrapper classes can affect method overload selection.

Example

public class HelloWorld {
 
    static void display(int value) {
        System.out.println("int");
    }
 
    static void display(Integer value) {
        System.out.println("Integer");
    }
 
    public static void main(String[] args) {
 
        int a = 10;
        Integer b = 20;
 
        display(a);
        display(b);
    }
}

Output

int
Integer

The primitive argument selects the int method, while the wrapper argument selects the Integer method.

Wrapper Classes and Arithmetic

Wrapper objects can participate in arithmetic because Java automatically unboxes them.

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Integer a = 10;
        Integer b = 20;
 
        int result = a + b;
 
        System.out.println(result);
    }
}

Output

30

Conceptually:

Integer → int
Integer → int
     ↓
 addition
     ↓
   int

Wrapper Classes with Generics

Wrapper classes are heavily used with Java generics.

This is valid:

ArrayList<Integer> numbers = new ArrayList<>();

This is invalid:

ArrayList<int> numbers = new ArrayList<>();

Similarly:

Map<String, Integer> marks = new HashMap<>();

Here:

String  → reference type
Integer → wrapper/reference type

Example

import java.util.HashMap;
import java.util.Map;
 
public class HelloWorld {
 
    public static void main(String[] args) {
 
        Map<String, Integer> marks = new HashMap<>();
 
        marks.put("Java", 90);
        marks.put("SQL", 85);
 
        System.out.println(marks);
    }
}

Output

{Java=90, SQL=85}

Character Wrapper Class

The wrapper class for char is:

Character

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Character ch = 'A';
 
        System.out.println(ch);
        System.out.println(Character.isLetter(ch));
        System.out.println(Character.isDigit(ch));
    }
}

Output

A
true
false

Character provides many useful methods.

Examples:

Character.isLetter()
Character.isDigit()
Character.isWhitespace()
Character.isUpperCase()
Character.isLowerCase()
Character.toUpperCase()
Character.toLowerCase()

Boolean Wrapper Class

The wrapper class for boolean is:

Boolean

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        Boolean value = true;
 
        System.out.println(value);
    }
}

Output

true

You can also parse a string:

boolean value = Boolean.parseBoolean("true");

Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        boolean value = Boolean.parseBoolean("true");
 
        System.out.println(value);
    }
}

Output

true

Byte, Short, Long, Float and Double

Java provides wrapper classes for all numeric primitive types.

PrimitiveWrapper
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

Example

Byte byteValue = 10;
Short shortValue = 20;
Integer intValue = 30;
Long longValue = 40L;
Float floatValue = 50.5f;
Double doubleValue = 60.5;

Primitive to Wrapper Conversion

You can explicitly use valueOf():

int primitive = 100;
 
Integer wrapper = Integer.valueOf(primitive);

Or rely on autoboxing:

int primitive = 100;
 
Integer wrapper = primitive;

Wrapper to Primitive Conversion

You can explicitly call the corresponding method:

Integer wrapper = 100;
 
int primitive = wrapper.intValue();

Or rely on unboxing:

Integer wrapper = 100;
 
int primitive = wrapper;

Complete Conversion Example

public class HelloWorld {
 
    public static void main(String[] args) {
 
        // Primitive
        int primitive = 100;
 
        // Autoboxing
        Integer wrapper = primitive;
 
        // Unboxing
        int anotherPrimitive = wrapper;
 
        // Wrapper to String
        String text = wrapper.toString();
 
        // String to primitive
        int parsedValue = Integer.parseInt("500");
 
        // String to wrapper
        Integer parsedWrapper = Integer.valueOf("600");
 
        System.out.println(primitive);
        System.out.println(wrapper);
        System.out.println(anotherPrimitive);
        System.out.println(text);
        System.out.println(parsedValue);
        System.out.println(parsedWrapper);
    }
}

Output

100
100
100
100
500
600

Wrapper Classes in Java Memory

A primitive variable directly stores its value.

For example:

int number = 100;

A wrapper variable is a reference to an object representing the value:

Integer number = 100;

Conceptually:

Primitive:
 
number
  |
  100
 
 
Wrapper:
 
number
  |
  ↓
Integer object
    |
    100

The exact JVM implementation and memory behavior is more detailed than this conceptual model, but the distinction is useful for understanding primitive versus reference types.

Performance Considerations

Primitive values are generally more lightweight than wrapper objects.

For example:

int number = 100;

does not require an Integer object.

Whereas:

Integer number = 100;

uses a reference type and may involve an object representation.

Therefore, when object behavior is not required, primitives are often preferable for simple numeric computations.

However, wrapper classes are necessary in many APIs, especially:

  • Collections
  • Generics
  • APIs requiring objects
  • Nullable values
  • Utility methods

Common Mistakes

1. Comparing wrappers using ==

Avoid:

Integer a = 1000;
Integer b = 1000;
 
if (a == b) {
    // ...
}

Use:

if (a.equals(b)) {
    // ...
}

For potentially null values, use a null-safe approach such as:

Objects.equals(a, b)

2. Unboxing a null wrapper

Avoid:

Integer number = null;
 
int value = number;

This causes NullPointerException.

3. Using wrapper classes unnecessarily

For simple calculations, this is often unnecessary:

Integer a = 10;
Integer b = 20;
Integer result = a + b;

If you do not need an object, a primitive may be simpler:

int a = 10;
int b = 20;
int result = a + b;

4. Using deprecated constructors

Avoid old code such as:

new Integer(10);

Prefer:

Integer.valueOf(10);

or:

Integer number = 10;

5. Confusing parseInt() and valueOf()

Remember:

Integer.parseInt("100")

returns:

int

while:

Integer.valueOf("100")

returns:

Integer

Wrapper Classes Quick Reference

WrapperCommon ConversionExample
ByteparseByte()Byte.parseByte("10")
ShortparseShort()Short.parseShort("10")
IntegerparseInt()Integer.parseInt("10")
LongparseLong()Long.parseLong("10")
FloatparseFloat()Float.parseFloat("10.5")
DoubleparseDouble()Double.parseDouble("10.5")
CharacterCharacter utility methodsCharacter.isLetter('A')
BooleanparseBoolean()Boolean.parseBoolean("true")

Primitive vs Wrapper Class

FeaturePrimitiveWrapper
ExampleintInteger
ObjectNoYes
Can store nullNoYes
Works directly with genericsNoYes
CollectionsNot directlyYes
Has utility methodsLimitedYes
AutoboxingN/ASupports conversion
UnboxingN/ASupports conversion
Generally lightweightYesMore overhead
Can call methodsNoYes

Important Points to Remember

  1. Every primitive type except void has a corresponding standard wrapper class.
  2. int → Integer.
  3. char → Character.
  4. boolean → Boolean.
  5. Wrapper classes are objects.
  6. Wrapper references can hold null.
  7. Autoboxing converts primitive → wrapper.
  8. Unboxing converts wrapper → primitive.
  9. Wrapper classes are required when primitives need to be used with Java generics and collections.
  10. Use equals() rather than == when comparing wrapper values.
  11. Be careful when unboxing null.
  12. parseInt() returns a primitive int.
  13. valueOf() returns an Integer.
  14. Wrapper classes provide useful constants such as Integer.MAX_VALUE.
  15. Modern Java code should avoid deprecated wrapper constructors such as new Integer(10).

Conclusion

Wrapper classes provide object representations of Java's primitive data types.

For example:

int     → Integer
long    → Long
double  → Double
char    → Character
boolean → Boolean

Their most important purpose is to allow primitive values to work in situations where Java requires objects, especially collections and generics.

Two important concepts associated with wrapper classes are:

Autoboxing:
primitive → wrapper
 
Unboxing:
wrapper → primitive

For example:

int number = 10;
 
Integer wrapper = number;  // Autoboxing
 
int value = wrapper;       // Unboxing

Understanding wrapper classes is important because they connect Java's primitive types with its object-oriented type system and are used extensively throughout the Java Collections Framework, Generics, APIs, and application development.

See you in next page....🙂

← EnumAutoboxing & Unboxing →
← EnumAutoboxing & Unboxing →