Wrapper Classes

Objects for Primitives

Each primitive type has a wrapper class in java.lang. Wrappers are needed because collections and generics work only with objects: you cannot write List<int>, only List<Integer>.

PrimitiveWrapperExample
byteByteByte.MAX_VALUE = 127
shortShort
intIntegerInteger.parseInt("42")
longLongLong.parseLong("9000000000")
floatFloat
doubleDoubleDouble.parseDouble("3.14")
charCharacterCharacter.isDigit('7')
booleanBooleanBoolean.parseBoolean("true")

Useful Constants and Methods

Example

public class Main {
    public static void main(String[] args) {
        System.out.println("int range: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
        System.out.println("max of 7, 3: " + Integer.max(7, 3));
        System.out.println("compare(5, 9): " + Integer.compare(5, 9));
        System.out.println("binary of 10: " + Integer.toBinaryString(10));
        System.out.println("hex of 255: " + Integer.toHexString(255));
        System.out.println("isLetter('a'): " + Character.isLetter('a'));
        System.out.println("toUpperCase('q'): " + Character.toUpperCase('q'));
        System.out.println("Double.isNaN(0.0/0): " + Double.isNaN(0.0 / 0));
    }
}

Output

int range: -2147483648 to 2147483647
max of 7, 3: 7
compare(5, 9): -1
binary of 10: 1010
hex of 255: ff
isLetter('a'): true
toUpperCase('q'): Q
Double.isNaN(0.0/0): true