Java Wrapper Classes
100%

Java Wrapper Classes

Wrapper classes in Java convert primitive data types into objects. They are useful when working with collections, generics, and methods that require objects instead of primitive values.

1. Introduction to Wrapper Classes

Java has primitive types such as int, double, char, and boolean. Each primitive type has a corresponding wrapper class.

int number = 10;
Integer object = Integer.valueOf(number);

System.out.println(number);
System.out.println(object);

Output:

10
10

The primitive variable stores a primitive value, while the Integer variable refers to an object that represents an integer value.

2. Primitive Types and Wrapper Classes

Primitive Type Wrapper Class
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble
charCharacter
booleanBoolean

Notice that the wrapper class for int is Integer, and the wrapper class for char is Character.

3. Creating Wrapper Objects

Use methods such as Integer.valueOf() to obtain wrapper objects from primitive values.

public class Main {
    public static void main(String[] args) {
        Integer number = Integer.valueOf(100);
        Double price = Double.valueOf(99.50);
        Character letter = Character.valueOf('A');
        Boolean status = Boolean.valueOf(true);

        System.out.println(number);
        System.out.println(price);
        System.out.println(letter);
        System.out.println(status);
    }
}

Output:

100
99.5
A
true

Wrapper classes are immutable. Once a wrapper object represents a value, that value cannot be changed inside the same object.

4. Autoboxing in Java

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

public class Main {
    public static void main(String[] args) {
        int number = 50;

        Integer object = number;

        System.out.println(number);
        System.out.println(object);
    }
}

Output:

50
50

Java automatically converts the int value into an Integer object in the assignment.

The following assignments also demonstrate autoboxing:

Integer a = 10;
Double b = 25.5;
Boolean c = true;
Character d = 'J';

5. Unboxing in Java

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

public class Main {
    public static void main(String[] args) {
        Integer object = 100;

        int number = object;

        System.out.println(object);
        System.out.println(number);
    }
}

Output:

100
100

Java automatically converts the Integer object into an int value during assignment.

Unboxing a null wrapper reference causes a NullPointerException. Check for null before unboxing when a wrapper value may be absent.

6. Parsing Strings into Numbers

Wrapper classes provide parsing methods that convert numeric text into primitive values.

public class Main {
    public static void main(String[] args) {
        String a = "100";
        String b = "25.5";

        int number = Integer.parseInt(a);
        double price = Double.parseDouble(b);

        System.out.println(number + 50);
        System.out.println(price + 10);
    }
}

Output:

150
35.5

Common parsing methods include:

  • Byte.parseByte()
  • Short.parseShort()
  • Integer.parseInt()
  • Long.parseLong()
  • Float.parseFloat()
  • Double.parseDouble()
  • Boolean.parseBoolean()

Invalid numeric text can cause a NumberFormatException.

7. Converting Strings into Wrapper Objects

Methods such as Integer.valueOf() can convert numeric strings into wrapper objects.

public class Main {
    public static void main(String[] args) {
        Integer number = Integer.valueOf("250");
        Double price = Double.valueOf("45.75");

        System.out.println(number);
        System.out.println(price);
    }
}

Output:

250
45.75

Use parseInt() when you need an int primitive and valueOf() when you need an Integer object.

8. Useful Wrapper Class Methods

Wrapper classes provide several helpful methods.

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);

        System.out.println(Double.MAX_VALUE);
        System.out.println(Integer.compare(10, 20));
    }
}

Important members include:

Member Purpose
Integer.MAX_VALUE Largest int value.
Integer.MIN_VALUE Smallest int value.
Integer.parseInt() Parses text into an int.
Integer.valueOf() Obtains an Integer from a value or numeric string.
Double.parseDouble() Parses text into a double.
Character.isLetter() Checks whether a character is a letter.
Character.isDigit() Checks whether a character is a digit.
Boolean.parseBoolean() Returns true for text equal to "true", ignoring case.

Output from the first example:

2147483647
-2147483648
1.7976931348623157E308
-1

9. Wrapper Classes with ArrayList

Java collections store objects rather than primitive values. This is why we use Integer instead of int in an ArrayList.

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> numbers = new ArrayList<>();

        numbers.add(10);
        numbers.add(20);
        numbers.add(30);

        int total = 0;

        for (int number : numbers) {
            total += number;
        }

        System.out.println("Total: " + total);
    }
}

Output:

Total: 60

The primitive int values are automatically boxed when added and unboxed when retrieved in the loop.

10. Comparing Wrapper Objects

The == operator compares object references when used with wrapper objects. Use equals() to compare their values.

public class Main {
    public static void main(String[] args) {
        Integer a = Integer.valueOf(1000);
        Integer b = Integer.valueOf(1000);

        System.out.println(a == b);
        System.out.println(a.equals(b));
    }
}

Output:

false
true

The two objects represent equal integer values, but they are distinct objects. Do not rely on == for wrapper value comparisons because some wrapper values may be cached.

11. Primitive Types vs Wrapper Classes

Primitive Types Wrapper Classes
Examples: int, double, char. Examples: Integer, Double, Character.
Store primitive values. Represent values as objects.
Cannot be null. Object references can be null.
Can be used directly in arithmetic. Can be automatically unboxed for arithmetic.
Can be used as array element types. Can be used as generic type arguments.
Remember: Prefer primitive types for ordinary calculations when an object is not required. Use wrapper classes when working with generics, collections, or values that may be absent.

12. Common Mistakes and Best Practices

  • Do not use primitive types as generic arguments; use wrapper classes.
  • Use equals() rather than == to compare wrapper values.
  • Handle invalid numeric strings when using parsing methods.
  • Check for null before unboxing a wrapper reference.
  • Remember that wrapper objects are immutable.
  • Prefer primitive types when object behavior is unnecessary.

Example of safe parsing:

String input = "123";

try {
    int number = Integer.parseInt(input);
    System.out.println(number);
} catch (NumberFormatException e) {
    System.out.println("Please enter a valid integer.");
}

13. Practice Assignments

  1. Display all eight primitive types and their wrapper classes.
  2. Convert an int value into an Integer object.
  3. Demonstrate autoboxing and unboxing.
  4. Convert the string "250" into an int using parseInt().
  5. Convert the string "45.75" into a double.
  6. Display Integer.MAX_VALUE and Integer.MIN_VALUE.
  7. Use Character.isDigit() to check a character.
  8. Create an ArrayList of Integer values and calculate their sum.
  9. Compare two Integer objects using equals().
  10. Handle invalid numeric input using NumberFormatException.

🧠 Quick Quiz

1. What is the wrapper class for int?




2. What is autoboxing?




3. Which method converts a numeric string into an int?