Shiklo Programming
EN
  • English
  • Español
  • Português
  • Deutsch
  • Français
  • 日本語
  • हिन्दी

Java Wrapper Classes

The eight primitive types are not objects: they have no methods and cannot be null. Many parts of Java, such as the collections in the next topics, work only with objects. For that reason each primitive type has a matching wrapper class, an object that holds one primitive value.

PrimitiveWrapper
intInteger
longLong
doubleDouble
floatFloat
booleanBoolean
charCharacter
byteByte
shortShort

Autoboxing and unboxing

Java converts between a primitive and its wrapper automatically. Putting a primitive into a wrapper is autoboxing, taking it out is unboxing:

import java.util.ArrayList;

public class Main {
    public static void main(String[] args) {
        Integer boxed = 42;          // autoboxing
        int plain = boxed + 8;       // unboxing, then addition
        System.out.println(plain);
        ArrayList<Integer> list = new ArrayList<>();
        list.add(10);                // the int 10 is boxed
        int first = list.get(0);     // and unboxed again
        System.out.println(first * 2);
    }
}
Output
50
20

The list in this example is an ArrayList; it can only hold objects, which is why it is written with Integer, not int.

Useful static methods

The wrapper classes contain many helpful methods. You already used Integer.parseInt in Type Casting and Integer.MAX_VALUE in Data Types:

public class Main {
    public static void main(String[] args) {
        System.out.println(Integer.parseInt("250") + 1);
        System.out.println(Double.parseDouble("2.5") * 2);
        System.out.println(Integer.toBinaryString(10));
        System.out.println(Integer.toHexString(255));
        System.out.println(Integer.max(4, 9));
        System.out.println(Character.isDigit('7'));
        System.out.println(Character.isLetter('x'));
        System.out.println(Character.toUpperCase('q'));
        System.out.println(Boolean.parseBoolean("TRUE"));
    }
}
Output
251
5.0
1010
ff
9
true
true
Q
true

The Character methods are especially handy for checking text one character at a time, for example to count the digits in a password.

Wrappers can be null

A wrapper variable is a reference, so it can be null. Unboxing a null stops the program with a NullPointerException, and Java's message tells you which value was null:

public class Main {
    public static void main(String[] args) {
        Integer stock = null;
        System.out.println("Stock is " + stock);
        int count = stock;
        System.out.println(count);
    }
}
Output
Stock is null
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Integer.intValue()" because "<local1>" is null
	at Main.main(Main.java:5)

Printing null is fine; doing arithmetic with it is not. When a value may be missing, check it first: if (stock != null).

Comparing wrapped numbers

Compare wrapper objects with equals, not ==. Java keeps one shared object for small values (from -128 to 127), so == happens to work for them and then suddenly fails for bigger numbers:

public class Main {
    public static void main(String[] args) {
        Integer a = 100, b = 100;
        Integer c = 1000, d = 1000;
        System.out.println(a == b);
        System.out.println(c == d);
        System.out.println(c.equals(d));
        System.out.println(c.intValue() == d.intValue());
    }
}
Output
true
false
true
true

This is the same rule as for strings: == compares objects, equals compares values.

Practice exercises

  1. Convert the texts "12", "30" and "8" to numbers and print their sum.
  2. Count the digits, letters and other characters in the text "Pass123!" using Character methods.
  3. Print the binary form of the numbers from 1 to 8.